tpalm
tpalm

Reputation: 17

How to print matching elements in different positions between two arrays? (java)

I have two arrays. Array1 holds 5 randomly generated numbers and array2 holds 5 guesses inputted by the user. I'm trying to count the matches but the only matches that are being read are the ones in the same position. How can I get my program to count the same number even if it's in a different position?

Here's what I've got so far:

int count = 0;
for (i=0;i<array1.length;i++){
   if(array1[i] == array2[i]){
      count = count +1;               
   } 
}
System.out.println("matching numbers  : "+count); 

Upvotes: 0

Views: 1357

Answers (3)

xleon90
xleon90

Reputation: 1316


You just need the intersection between the two arrays and then to count the size of the result array. So you can avoid to manually loop over the two arrays just simply using the retainAll method on List class:

https://docs.oracle.com/javase/7/docs/api/java/util/List.html#retainAll

Here is a junit test that shows how to solve using this approach:

@Test
public void TestArraysIntersection() {
        Integer[] randomlyGenerated = {1,2,3,4,5};
        Integer[] userInput = {4,2,5,3,6};

        System.out.println("Randomly generated numbers are: " + Arrays.toString(randomlyGenerated));
        System.out.println("Number selected by the user are: " + Arrays.toString(userInput));

        List<Integer> intersectionList = new ArrayList<>(Arrays.asList(randomlyGenerated));
        intersectionList.retainAll(Arrays.asList(userInput));
        System.out.println("Matching numbers are " + intersectionList.size() + " and the values are: "+ intersectionList);
}

Test result is the following:

Randomly generated numbers are: [1, 2, 3, 4, 5]
Number selected by the user are: [4, 2, 5, 3, 6]
Matching numbers are 4 and the values are: [2, 3, 4, 5]

Upvotes: 2

JustAnotherDeveloper
JustAnotherDeveloper

Reputation: 2256

You need to loop through both arrays. In your code you are comparing each element of one array with the element in the same position of the other array, but you have to compare each element of one array with every element of the other array, like this:

public class MyClass {
    public static void main(String args[]) {
      int[] numbers = {1, 3, 0, 6};
      int[] guesses = {3, 8, 5, 1, 2};
      for (int i = 0; i < numbers.length; i++) {
          for (int j = 0; j < guesses.length; j++) {
              if (numbers[i] == guesses[j]) {
                  System.out.println("A match on positions "+i+" and "+j+". "+numbers[i]+" = "+guesses[j]);
              }
          }
      }
    }
}

Output:

A match on positions 0 and 3. 1 = 1

A match on positions 1 and 0. 3 = 3

Of course, instead of outputting the values that match, you can instead increment a count like in your example, and show instead how many elements matched.

Upvotes: 1

Abra
Abra

Reputation: 20914

If the two arrays are both small, i.e. each array contains only five elements, then you need a nested loop. For each element in the random numbers array, iterate through the guesses array.

int count = 0;
for (int i = 0; i < array1.length; i++) {
    for (int j = 0; j < array2.length; j++) {
        if (array1[i] == array2[j]) {
            count++;
        }
    }
}
System.out.println("matching numbers  : "+count);

Note that the above is appropriate when both arrays are small. When both arrays are large the above is not appropriate.

Upvotes: 3

Related Questions