Remove duplicate from sorted array

array + hashmap

Problem:

Remove duplicate from sorted array

Solution:

public int[] removeDuplicate(int[] array){
    if(array == null || array.length == 0) return null;
    HashMap<Integer, Integer> map = new HashMap<>();
    int count = 0;
    ArrayList<Integer> rew = new ArrayList<>();
    for(int i = 0; i < array.length; i++){
        if(map.containsKey(array[i])){
            count = map.get(array[i]) + 1;
            map.put(array[i],count);
        }else{
            map.put(array[i], 1);
        }
    }
    for(int i = 0; i < array.length; i++){
       if(map.get(array[i]) == 1){
       res.add(array[i]);
       }
   }   
   return res.toArray();

}

Last updated

Was this helpful?