So I was asked to have two different arrays that both stored 5 random numbers and if any number between each array were the same, they would be printed out.
// Creates Arrays
for (int i = 0; i < 5; i++)
{
first5num[i] = rand.nextInt(9 + 1);
System.out.print(" " + first5num[i]);
}
for (int j = 0; j < 5; j++)
{
second5num[j] = rand.nextInt(9 + 1);
System.out.print(" " + second5num[j]);
}
//Loops through arrays to find duplicates
for (int k = 0; k < 5; k++)
{
comparitive = first5num[k];
for (int l = 0; l < 5; l++)
{
if (comparitive == second5num[l])
{
SameNums += comparitive;
}
}
}
I was able to get it working and it would print out the shared numbers between both arrays. I stored the Shared numbers into a string called SameNums and now I must ensure that the string does not hold any duplicate numbers. For example Array1 = 1, 2, 3, 4, 5 and Array2 = 1, 2, 3, 5, 5. The SameNums string would hold "12355". How do I ensure that 5 is only listed once and please keep in mind that the string may include duplicates that are not directly next to each other (12928 should get rid of the second 2. Below is my train of thought so far:
//Manipulates string using while loop
while (run == true)
{
if (counter == SameNums.length() - 1)
{
run = false;
}
if (SameNums.charAt(counter) == SameNums.charAt(restrict))
{
word1 = SameNums.substring(0, restrict);
word3 = SameNums.substring(restrict + 1, SameNums.length());
SameNums = word1 + word3;
counter++;
restrict = counter + 1;
continue;
}
How can I make the string read no duplicate chars?*