O(n) java solution


  • 0
    M
    
    class Solution {
        public int[] singleNumber(int[] nums) {
            Arrays.sort(nums);
            int index= 0;
            int[] newArr = new int[2];
            for(int i=0;i<nums.length-1;){
                if(nums[i] == nums[i+1]){
                    i = i+2;
                }else{
                    newArr[index] = nums[i];
                    index++;
                    i++;
                }
            }
            if(index==1){
                newArr[index]=nums[nums.length-1];
            }
            
            return newArr;
        }
    }

Log in to reply
 

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