Java code (5 ms)


  • -7
    S

    public int singleNumber(int[] nums) {

        if(nums.length==1)
        return nums[0];
        
        Arrays.sort(nums);
        
        int value=nums[0];
        
        int count=1;
        
        for(int i=1;i<nums.length;i++)
        {
            if(value==nums[i])
            count++;
            else
            {
                if(count!=3)
                return value;
                else
                {
                    count=1;
                    value=nums[i];
                }
                
            }
            
        }
        
        return value;
    }

  • 0
    A

    Sorting defeats the purpose of this question.


  • 0
    X

    sorting at least n*log(n), so it is not O(n) time complexity.


Log in to reply
 

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