Created
June 18, 2017 17:52
-
-
Save thmain/b37a905e906c33e7d148ca868aa458b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to handle 0