How to find duplicate number in Integer array in Java?

[code lang=”java”]
public class DuplicateInArray {

public static void main(String[] args) {
System.out.println(isDuplicate(new int[]{2, 16, 9, 2, 6}));
}

public static boolean isDuplicate(int[] nums) {
boolean status = false;
Arrays.sort(nums);
int i = 0;
while (i < nums.length – 1) {
if (nums[i] == nums[i + 1]) {
status = true;
}
i++;
}
return status;
}
}
[/code]
Output:
true

Tagged on:     

Leave a Reply

X