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?