# Printing repeating integers in a given array?

• 01-22-2011, 06:16 AM
baumboards
Printing repeating integers in a given array?
I am given a set of integers in an array, I need to print out the repeating integers and their indices. Thus far my code prints out every single comparison, what I need it to do is to only print out the index of the values that do match.

public class FindDuplicate {

public static void main(String[] args) {

int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2};

int i,j;

for( i = 0;i<arr.length;i++){
for( j = i + 1; j<arr.length;j++){

if(arr[i] == arr[j]);

System.out.println("Index " + i +" same as Index "+j+" with value "+ arr[i]);

}
}
}
}
• 01-22-2011, 08:08 AM
pbrockway2
Use braces for every if statement - even those that execute a single line of code. That will help you catch problems like this.

(If you take this advice and repost your code if it still doesn't behave as you expect, use the code tags. Put [CODE] at the start of the code and [/CODE] at the end. That way the formatting will be preserved.)
• 01-22-2011, 10:50 PM
baumboards
Your suggestion did work, by adding the braces around both the for-statements and if-statements the program now prints out only one output, albeit a wrong output, but still one output vs. many incorrect outputs. Here is the improved code that still doesn't work completely:
Code:

public class FindDuplicate {

public static void main(String[] args) {

int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2};

int i,j=0;
{
for(i = 0;i<arr.length-1;i++)
{  for(j = i+ 1; j<arr.length;j++)
{
if(arr[i] == arr[j]);

}
}
System.out.println("Index " + i +" same as Index "+j+" with value "+ arr[i]);

}
}
}

• 01-22-2011, 11:33 PM
pbrockway2
Personally I find that even harder to read. I was suggesting using braces with the one line if statement. Putting the opening brace at the end of the line as you did originally you would get:

Code:

public class FindDuplicate {

public static void main(String[] args) {

int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2};

int i,j;

for( i = 0;i<arr.length;i++){
for( j = i + 1; j<arr.length;j++){

if(arr[i] == arr[j]);{ // <-- this is the line that's wrong

System.out.println("Index " + i +" same as Index "+j+" with value "+ arr[i]);

}
}
}
}
}

The semicolon ; should not be there.
• 01-22-2011, 11:57 PM
baumboards
Haha leave it to a java veteran to solve my problem with the simplest of solutions. The code works just fine now. How is it that a semi-colon made all the difference?
• 01-23-2011, 12:24 AM
pbrockway2
Quote:

How is it that a semi-colon made all the difference?

Code:

if(arr[i] == arr[j]);
{
System.out.println("Index " + i +" same as Index "+j+" with value "+ arr[i]);
}

The first line here is an empty if statement. It is saying "if the two array values are equal do nothing".

The one line block that follows forms a statement that is completely separate from the if statement. It will be executed whatever the value of arr[i]==arr[j] is, which is why you were seeing all that output.

The reason I suggested using braces for the one line if statement is that a line like:

Code:

whatever);{

is never what you mean. The ; ends one statement, and the { begins a new one. Since we don't put two statements on one line (or shouldn't) this should strike the eye as an error.
• 01-24-2011, 06:54 AM
baumboards
Thank you again for explaining that to me.
• 01-24-2011, 07:08 PM
pbrockway2
You're welcome.