Accepted Java Solution with HashMap


  • 0
    P
    public int findDuplicate(int[] nums) {
            int n=nums.length-1;
            HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
            for(int i=1;i<=n;i++){
                hm.put(i,0);
            }
            for(int t=0;t<nums.length;t++){
                if(hm.containsKey(nums[t])){
                    hm.put(nums[t],hm.get(nums[t])+1);
                    
                }
            }
            int ans=0;
            for(int k=1;k<=n;k++){
                if(hm.get(k)>1) ans=k;
                
            }
            return ans;
        }
    

  • 0
    T

    This solution with Space O(n)


Log in to reply
 

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