Hash Map implementation in Java for Duplicate Values with O(n) time complexity.


  • 0
    V
    class Solution {
        public boolean containsDuplicate(int[] nums) 
        {
        		Map<Integer,Integer> maps = new HashMap<>();
        		
        		for (int i = 0; i<nums.length;i++)
        		{
        			maps.put(nums[i], maps.getOrDefault(nums[i],0)+1);
        			if (maps.containsKey(nums[i]) && (maps.get(nums[i])>1))
        			{
        				return true;
        			}
        		}
        		
        		return false;
            
        }
    }
    

Log in to reply
 

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