Easy to understand Java Solution- beat 100% of java submissions...Maybe just lucky


  • 0
    S
     public int[] findErrorNums(int[] nums) {
              int[] result=new int[] {-1,-1};
    	        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
    	        for(int i=0;i<nums.length;i++) {
    	        	if(map.containsKey(nums[i]))
    	        	{
    	        		result[0]=nums[i];
    	        	}
    	        	map.put(nums[i], map.getOrDefault(nums[i], 0)+1);
    	        }
    	        for(int i=nums.length;i>=1;i--) {
    	        	if(!map.containsKey(i)) {
    	        		result[1]=i;
    	        		break;
    	        	}
    	        }
    	        
    	        return result;
        }
    

Log in to reply
 

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