My Java solution


  • 0
    K
    public List<Integer> findDuplicates(int[] nums) {
            Set<Integer> set = new HashSet<>(nums.length);
    	List<Integer> res = new ArrayList<Integer>();
    	for(int i = 0; i < nums.length; i++) {
    		if(set.contains(nums[i])) {
    			res.add(nums[i]);
    		}else {
    			set.add(nums[i]);
    		}
    	}
    	return res;
    }
    

Log in to reply
 

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