Arif Nadeem
Arif Nadeem

Reputation: 8604

What is wrong with this code?

I coded this in attempt to find the duplicates in an array and increment the count each time a duplicate element was found, this program work but if I put an else statement after if statement the compiler prints off the else statement even though the array has duplicate elements...

  public class arraysexpmnt {
  public static void main(String[] args) {
    int[] arr={2,2,2,5,7,8,9,9,8,7};
    int count=0;
    for(int i=0;i<arr.length;i++){
        for(int j=i+1;j<arr.length;j++){
            if(arr[j]==arr[i]){
                count++;
                 System.out.println("Duplicate found! Original is " + arr[i] + " and match is " +arr[j]+" and the count of similar elements is "+count);
            }

        }

    }


}

}

Upvotes: 0

Views: 151

Answers (4)

Atreys
Atreys

Reputation: 3761

Your code outputs:

Duplicate found! Original is 2 and match is 2 and the count of similar elements is 1 
Duplicate found! Original is 2 and match is 2 and the count of similar elements is 2
Duplicate found! Original is 2 and match is 2 and the count of similar elements is 3
Duplicate found! Original is 7 and match is 7 and the count of similar elements is 4
Duplicate found! Original is 8 and match is 8 and the count of similar elements is 5
Duplicate found! Original is 9 and match is 9 and the count of similar elements is 6

If your array was sorted, then you could increment i whenever you find a matching j. This would prevent the extra reports for more-than-3 situations. You may also want to reset count on each i iteration.

Your array is not completely sorted, though. Another approach may be desirable if you want to eliminate the duplication.

Upvotes: 0

Xavier Combelle
Xavier Combelle

Reputation: 11195

I think the code you look for is:

  public class arraysexpmnt {

     public static void main(String[] args) {
       int[] arr={2,2,2,5,7,8,9,9,8,7};
       int count=0;
       for(int i=0;i<arr.length;i++){
          boolean found = False;
          for(int j=i+1;j<arr.length;j++){
             if(arr[j]==arr[i]){
                count++;
                System.out.println("Duplicate found! Original is " + arr[i] + " and match is " +arr[j]+" and the count of similar elements is "+count);
                found = True;
             }
           }
           if (!found) {
                System.out.println("No duplicate found for  Original: " + arr[i] );
           }
        }

     }
  }

Upvotes: 1

love_me_some_linux
love_me_some_linux

Reputation: 2741

The code looks fine to me. If your code does not find duplicate elements in the current loop, it should execute the else clause as you say it is doing. If that is not the functionality that you want, then you have to change your logic. What is it that you want your code to do?

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96258

the else clause gets executed whenever in the loop the two elements in the array do not match.. which is a quite common thing. put the same tracing println there and you'll see it.

Upvotes: 1

Related Questions