My java solution using HashMap


  • 0
    W

    public class Solution {

     public List<Integer> findDuplicates(int[] nums) {
             
         Map<Integer,Integer> map = new HashMap<>();
         List<Integer> result = new ArrayList<>();
    
          for(int i=0;i<nums.length;i++){
        	  if(map.containsKey(nums[i])){
        		  result.add(nums[i]);
        	  }
        	  else{
        		  map.put(nums[i], 1);  
        	  }	    		 
          }
          return result;
     }
    

    }


Log in to reply
 

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