class RemoveDuplicates{
public static int[] removeDuplicates (int[] a){
HashMap<Integer,Boolean> hmap = new HashMap<Integer,Boolean>();
int count = 0;
for(int i=0;i<a.length;++i){
if(hmap.containsKey(a[i])== false){
hmap.put(a[i],true);
a[i-count] = a[i];
}
else{
count++;
}
}
int[] res = java.util.Arrays.copyOfRange(a, 0, a.length-count);
return res;
}
public static void main(String[] args) {
int[] arr = {1,1,2,2,3,4,4,5,5,5,1,1,3,6};
int[] res = removeDuplicates(arr);
System.out.println(Arrays.toString(res));
}
}
No comments:
Post a Comment