My java solution to an unsorted array with hashset and two pointers


  • 0
    C
        public int removeDuplicates(int[] nums) {
            if(nums.length==0||nums.length==1)return nums.length;
            HashSet<Integer> hs=new HashSet<Integer>();
            int i=0;
            int j=0;
            while(j<nums.length){
                if(hs.add(nums[j])){
                    nums[i++]=nums[j++];
                }else{
                    j++;
                }
            }
                return i;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.