Finding duplicate numbers in two arrays

Viewed 199

I have been asked to make a program that can find duplicate numbers within two arrays. I have figured out how to get the duplicate numbers to print out but I am having a problem getting them to print out with only one number of the duplicated. ie;

int[] array1 = new int[2,4,6,8,10,12,14,16,18,20]
int[] array1 = new int[2,4,6,8,10,10,6,7,20,20]

output:

2, 4, 6, 8, 10, 10, 6, 20, 20

How would I get it to only output one 20 and one 10?

import java.security.SecureRandom;

public class Duplicates 
{

    public static void main(String []args)
    {
        int[] array1 = new int[10];
        int[] array2 = new int[10];

        SecureRandom randomNumbers = new SecureRandom();

        for(int i = 0; i < array1.length; i++)
        {
            array1[i] = randomNumbers.nextInt(19);
        }
        for(int i = 0; i < array2.length; i++)
        {
            array2[i] = randomNumbers.nextInt(19);
        }
        System.out.printf("Array 1: ");
        for(int i: array1)
        {
            System.out.print(i + ",");
        }
        System.out.println();

        System.out.printf("Array 2: ");

        for(int i: array2)
        {
            System.out.print(i + ",");
        }

        System.out.println("\nValues the exist in both arrays are... ");

        for(int i = 0; i < array1.length;i++)
        {
            for(int j = 0; j < array2.length; j++)
            {
                if(array1[i]==array2[j])
                {
                    System.out.printf(array1[i] + ",");
                }
            }
        }
    }
}
3 Answers

You need a way of logging which numbers have already been identified as duplicates.

I suggust: make a new collection, e.g. a HashSet, and add duplicate values to that instead of printing them. Once you have gone through all values in the original arrays, print whatever is in the duplicates collection.

You can create a list named duplicates, and to that list add all duplicates values. But before adding check if that list already contains that value.

public class Main
{
    public static void main(String[] args)
    {
        int[] array1 = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
        int[] array2 = {2, 4, 6, 8, 10, 10, 6, 7, 20, 20};

        System.out.println("Array 1: " + java.util.Arrays.toString(array1)); // faster way to print array
        System.out.println("Array 2: " + java.util.Arrays.toString(array2));

        List<Integer> duplicates = new ArrayList<>(); // list for all duplicates

        for (int k : array1)
        {
            if (duplicates.contains(k)) // check if You have already added this value to duplicates
            {
                break;
            }

            for (int i : array2)
            {
                if (k == i)
                {
                    duplicates.add(k);
                    break;
                }
            }
        }

        System.out.println("Duplicates: " + duplicates); // [2, 4, 6, 8, 10, 20]
    }
}

I am not sure but I think this is not the most efficient way to search duplicates in two lists. Maybe sorting both arrays and then searching duplicates would be better. Your two nested loops will run very long in bigger data sets. (but it's just my opinion, I'm not sure)

Here is another way. Just convert the two arrays to Set<Integer> and use Set#retainAll. I am using more diverse arrays to demonstrate.

Integer[] array1 = { 2, 3, 1, 9, 7, 10, 11, 2, 4, 5, 10 };
Integer[] array2 = { 2, 10, 5, 19, 20, 7, 19, 8, 7, 6 };
        

Set<Integer> set1 = new HashSet<>(List.of(array1));
Set<Integer> set2 = new HashSet<>(List.of(array2));

set2.retainAll(set1);

System.out.println(set2);

Prints

[2, 5, 7, 10]

If you want to use primitive arrays, you can just create the sets and copy in the values.

And here is a more traditional way of doing it.

int[] array1 = { 2, 3, 1, 9, 7, 10, 11, 2, 4, 5, 10 };
int[] array2 = { 2, 10, 5, 19, 20, 7, 19, 8, 7, 6 };

Create a set of one of the source arrays.

Set<Integer> set = new HashSet<>();
for (int i : array1) {
    set.add(i);
}

Create an empty set to hold the duplicates

Set<Integer> dups = new HashSet<>();

Now iterate thru the other array. If an element is found in the previously created set, add it to the set of duplicates

for(int i : array2) {
    if (set.contains(i)) {
        dups.add(i);
    }
}

System.out.println(dups);

Prints

[2, 5, 7, 10]

Using sets here has two advantages.

  • contains for sets is efficient since it only needs to check the bucket associated with the hashcode of the object one is trying to find.
  • when accumulating duplicates, multiple duplicates will only be recorded once
Related