Java O(n) simple solution using array - 8 lines


  • 0
    M
    public int[] findErrorNums(int[] nums) {
            int[] res = new int[2];
    	int[] count = new int[nums.length + 1];
    	for (int i = 0; i < nums.length; i++) 	count[nums[i]]++;
    	for (int i = 1; i < count.length; i++) {
    	     if (count[i] == 2) res[0] = i;
                 if (count[i] == 0) res[1] = i;
    	}
    	return res;
    }
    
    

Log in to reply
 

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