Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 18, 2017 17:50
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/37676c716472619b521cb13e09ffa791 to your computer and use it in GitHub Desktop.
Save thmain/37676c716472619b521cb13e09ffa791 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.HashMap;
public class CheckDuplicatesUsingHashMap {
public void hasDuplicatesUsingMap(int [] arrA){
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i <arrA.length ; i++) {
if(map.containsKey(arrA[i])){
System.out.println("Array has duplicates : " + Math.abs(arrA[i]));
}else{
map.put(arrA[i], 1);
}
}
}
public static void main(String[] args) {
int a [] = {1, 6, 5, 2, 3, 3, 2};
new CheckDuplicatesUsingHashMap().hasDuplicatesUsingMap(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment