Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class CheckDuplicates {
public void hasDuplicates(int[] arrA) {
for (int i = 0; i < arrA.length; i++) {
//check if element is negative, if yes the we have found the duplicate
if (arrA[Math.abs(arrA[i])] < 0) {
System.out.println("Array has duplicates : " + Math.abs(arrA[i]));
} else {
arrA[Math.abs(arrA[i])] = arrA[Math.abs(arrA[i])] * -1;
}
}
}
public static void main(String[] args) {
int a[] = {1, 6, 5, 2, 3, 3, 2};
new CheckDuplicates().hasDuplicates(a);
}
}
@gururchandran

This comment has been minimized.

Copy link

@gururchandran gururchandran commented Jun 26, 2017

how to handle 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment