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] + ",");
}
}
}
}
}