tags:

views:

304

answers:

4

I have one array

int a[] = {1,2,3};
int b[] = {1,2};

how will I find the odd element in array

A: 

Make a loop for each array item. Those for which item & 1 == 1 are odd.

Konamiman
or item % 2 = 0
Paolo
Probably, you mean item % 2 == 1
Vanya
Konamiman
So? What's the downvote for?
Konamiman
+1  A: 
    int[] longArray = { 1, 3, 2 };
 int[] shortArray = { 1, 2 };

 //Check which array is longer, if b longer than a then swap

 boolean found = false;
 int odd = 0;

 for (int i : longArray) {
  for (int j : shortArray) {
   if (i == j)
    found = true;
  }
  if (!found)
   odd = i;
  found = false;
 }
 System.out.println(odd);
medopal
A: 

If you are talking about Set Comparisons, have a look at java-is-there-an-easy-quick-way-to-and-or-or-xor-together-sets

This SO question was talking in terms of the Set interface, but the List interface also inherits the relevant methods from Collection, so copying your arrays to ArrayList objects makes it easy.

If you want to keep it purely at the native array level, you might want to do something like:

 public int[] findOddElement(int[] fromArray, int[] secondArray){
  int[] result = new int[fromArray.length];
  int resPointer = 0;
  for (int i = 0;i < fromArray.length;i++){
   boolean notFound = true;
   for (int j = 0; j < secondArray.length; j++) {
    if (fromArray[i] == secondArray[j]) {
     notFound = false;
     break;
    }
   }
   if (notFound){
    result[resPointer] = fromArray[i];
    resPointer++;
   }
  }
  if (resPointer > 0 && resPointer < fromArray.length ) {
   int[] newResult = new int[resPointer];
   for (int i = 0;i < resPointer; i++) {
    newResult[i] = result[i];

   }
   return newResult;
  }
  return result;
 }
Steve De Caux
ArrayList does not implement the Set interface. List does not extend the Set interface. List and Set are two different extensions of the Collection interface.
ILMTitan
Yes, my bad: The features used in the XOR discussion are inherited by both Set and List from the Collection interface. Edited answer.
Steve De Caux
+2  A: 
    int[] longArray = { 1, 3, 2 };
    int[] shortArray = { 1, 2 };

    //Check which array is longer, if b longer than a then swap

 for (int x:longArray){
        set.add(x);
 }
 for (int x:shortArray){
     if (set.contains(x))
        set.remove(x);
 }
 //odd numbers
 for (Object i:set.toArray())
  System.out.println(i+",");

I put two answers for the voting, the previous is nested loops and might be slow for very long arrays. In the next solution some might dont prefer the TreeSet.

medopal
hey i tried to take List instead of set but I am getting runtime Exception IndexOutOfBoundException
Vipul
The exception occurs in the remove method, since it takes an "index" if you're passing in int (which is the case here), if you want to use remove like the one in Sets then you will need to pass an Object say Integer, but in this case it will not work, because Integer(1)!=Integer(1). Back to the main point, Lists allows duplicates. Sets don't. So aside from the "remove implementation" you better stick to Sets because of the duplicates as well.
medopal