-6

In an java interview I was asked that how would you code to compare Array elements with each other to find out how many elements or indexes are equals and How many elements are not equal? Is there a way I can compare Array values in Java without using a for or while loop? The first thing that came in my mind that Arrays class provides us utility methods equals() and deepEquals() , but at last I was not able to make up, please advise with a small example that how can I solve this problem?

 int[] i1 = new int[] {1,2,3,4};
 int[] i2 = new int[] {0,5,3,3};
Community
  • 1
  • 1
tuntun fdg
  • 491
  • 2
  • 8
  • 12
  • 6
    Have you tried anything? This looks like a simple loop will do the job. – jlordo Apr 12 '13 at 08:35
  • Your question doesn't show any efforts. Its a simple thing you can find by goggling.Please read the faq's http://stackoverflow.com/faq before asking any question – Nikhil Agrawal Apr 12 '13 at 08:39
  • Not that everybody gets sent to Google for every question, but SO isn't ment to do your homework neither. – Joetjah Apr 12 '13 at 08:40
  • @tuntun This question is asked several time. Atlease you can refer stackoverflows old posts. stackoverflow.com/questions/2665593/how-to-compare-two-arrays-of-integers-order-insensitively – Nikhil Agrawal Apr 12 '13 at 08:43
  • There are so many grammar errors in the above comments it's not even funny. – imulsion Apr 12 '13 at 08:48
  • @imulsion thanks a lot , I will take care in future – tuntun fdg Apr 12 '13 at 08:49
  • @tuntunfdg not in your question, i presume english is not your first language, but in the above comments – imulsion Apr 13 '13 at 09:31

3 Answers3

3

Something like

notEqualCount += Math.abs(i1.length - i2.length);
for(int i=0; i<i1.length && i < i2.length;i++){
  if(i1[i]==i2[i]){
    equalCount++;   
  } 
  else{
    notEqualCount++; 
  }
}
Joetjah
  • 6,292
  • 8
  • 55
  • 90
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
1

Disclaimer: Did not even bother to compile this and omit any error handling:

/*Returns the equal count in int[0] and nonEqualCount in int[1] of the result*/
public static int[] findEqualAndNotEqual(Integer firstArray, Integer secondArray){  
   Set<Integer> set = new HashSet<Integer>(Arrays.asList(firstArray));    
   int equalCount = 0;  
   int nonEqualCount = 0;  
   for(Integer num:secondArray){  
      if(set.contains(num)){
         equalCount++;   
      }
      else{
         nonEqualCount++;
     }  
   }
   return new int[]{equalCount, nonEqualCount};  
}
Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • @AmitG:I am not planning to fix it and said I did not compile it.It is an interview question so it should be enough of the OP – Cratylus Apr 12 '13 at 08:56
0

You could loop through the arrays. Something like:

int[] array1  =new int[] {1,2,3,4};
int[] array2 = new int[] {0,5,3,3};
int numberOfEqualElements = 0;

for (int i=0; i<array1.length; i++) {
   if (array1[i] == (array2[i])) {
       numberOfEqualElements += 1;
   }
}
system.out.println("The number of elements that are equal is " + numberOfEqualElements);

Then you would know that the number that are not equal is whatever is left.

Ben Green
  • 3,953
  • 3
  • 29
  • 49
  • 3
    Please use your IDE to code, and copy/paste it here. This way you'll see the compiler errors you are posting... – jlordo Apr 12 '13 at 08:38
  • @jlordo Thanks for that! Was trying to do it without the help of eclipse to help me learn, but I should have checked before I posted it. :) – Ben Green Apr 12 '13 at 08:41