Java solution using sorting and total sum


  • 0
    S
    public class Solution {
        public int[] findErrorNums(int[] nums) {
            Arrays.sort(nums);
            int sum = 0;
            int sum1 = nums[0];
            int[] res = new int[2];
            for(int i = 1; i<nums.length; i++){
                if(nums[i] == nums[i-1]){
                    res[0] = nums[i];
                }
                sum1 = sum1 + nums[i];
            }
            sum = (nums.length*(nums.length + 1))/2;
            res[1] = (sum - sum1) + res[0];
            
            return res;    
        }
    }
    

Log in to reply
 

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