-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};
7
  • 6
    Have you tried anything? This looks like a simple loop will do the job. Commented Apr 12, 2013 at 8:35
  • Your question doesn't show any efforts. Its a simple thing you can find by goggling.Please read the faq's stackoverflow.com/faq before asking any question Commented Apr 12, 2013 at 8:39
  • Not that everybody gets sent to Google for every question, but SO isn't ment to do your homework neither. Commented Apr 12, 2013 at 8: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 Commented Apr 12, 2013 at 8:43
  • There are so many grammar errors in the above comments it's not even funny. Commented Apr 12, 2013 at 8:48

3 Answers 3

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++; 
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't i1[1] have to be i1[i] here?
I would change the condition to i < i1.length && i < i2.length to be safe with arrays of different length.
Just count the equals ones. The unequals ones are array length (or last value of i counter) minus the equals ones.
I've added the code to reflect PierreHenry and jlordo suggestions
Just came from a break. Thanks everyone for suggestion and edits. I was in hurry so added "Something like" at the start of my answer :)
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};  
}

1 Comment

@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
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.

2 Comments

Please use your IDE to code, and copy/paste it here. This way you'll see the compiler errors you are posting...
@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. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.