Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 18, 2017 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/b37a905e906c33e7d148ca868aa458b9 to your computer and use it in GitHub Desktop.
Save thmain/b37a905e906c33e7d148ca868aa458b9 to your computer and use it in GitHub Desktop.
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
Copy link

how to handle 0

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