0

I am beginner in Java and have recently switched to Java from C, I made this program to search for duplicate values but each time I run the program I get output as : "Duplicate value found"

My code:

public class Duplicate {

  public static void main(String[] args) {
    boolean found;
    int[] nums = { 184, 254, 123, 654, 146, 392 };
    int x, i;
    for (i = 0; i < 6; i++) {
      for (x = 1; x < 5; x++) {
        if (x != i && nums[x] == nums[i])
          found = true;
      }
    }
    if (found = true)
      System.out.println("Duplicate value found ");
    else
      System.out.println("Duplicate value not found ");
  }

}

here there is no duplicate value yet it displays the output as

Duplicate value found

which is not the case here. can you please point out my mistake here in this code?

1
  • I just need hint or correction in this code . I don't need entire program. Thank You. Commented Oct 21, 2015 at 13:56

4 Answers 4

1

if(found = true) means you are assigning the true to found variable, and it will always return true. That's why it is always printing the Duplicate value found. Change it to if(found==true)

Sign up to request clarification or add additional context in comments.

1 Comment

oh yes! i made the required changes and got the answer , wont repeat this mistake again. Thanx
0

I think that you forgot found = false on the beginning. You use = instead of == in your second if.

4 Comments

i tried this public class Duplicate { 'public static void main(String[] args) { boolean found = false; int[] nums = { 184, 254, 123, 654, 184, 392 }; int x, i; for (i = 0; i < 6; i++) { for (x = 1; x < 5; x++) { if (x != i && nums[x] == nums[i]) found = true; } } if (found == true) System.out.println("Duplicate value found "); else System.out.println("Duplicate value not found "); }' } thank you sir ! :D it worked.
@Alok You're welcome, don't forget to accept the answer.
@Alok Yet, you didn't^^
sorry i didnt know how to . Now i did. :)
0

I don't know what you are trying to achieve but perhaps instead of using Array you could use Set or Map? Set doesn't allow for any duplicate values and Map doesn't allow duplicate keys.

2 Comments

i will use them when i learn about them , as i said i am new to java and this site so i used all that i knew about.
and program worked now . with few adjustments in code
0

@Kotshi i tried this

public class Duplicate {
    public static void main(String[] args) {
        boolean found = false;
        int[] nums = { 184, 254, 123, 654, 184, 392 };
        int x, i;
        for (i = 0; i < 6; i++) {
            for (x = 1; x < 5; x++) {
                if (x != i && nums[x] == nums[i])
                    found = true;
            }
        }
        if (found == true)
            System.out.println("Duplicate value found ");
        else
            System.out.println("Duplicate value not found ");
    }
}

thank you sir ! :D it worked.

2 Comments

Please add a comment to the answer. Don't post your comment as a answer.
@knut sorry didnt know. i am new here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.