What's wrong with my code using HashMap? Time Limit Exceeded


  • 0
    H
    public class Solution {
        public boolean containsDuplicate(int[] nums) {
             int n = nums.length;
             HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
            
             for(int i = 0; i < n; i++){
                 if(hashMap.containsKey(nums[i])){
                     return true;
                 }else{
                     hashMap.put(nums[i], i);
                 }
             }
             return false;
        }
    }

  • 0
    Z

    Why you use HashMap, HashSet should be enough.


Log in to reply
 

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