My simple solution, 8 lines and easy to understand!


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

  • 0
    C

    Why not set instead of map ?


  • 1
    J
    public class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> pool = new HashSet<Integer>();
        for(int i=0; i<nums.length; i++) if(!pool.add(nums[i])) return true;
    return false;
    }
    

    }


Log in to reply
 

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