My Accepted Solution without using extra memory.


  • -4
    K
    public int singleNumber(int[] A) 
    {
        if (A.length == 1)
        {
            return A[0];
        }
        
        Arrays.sort(A);
        int result = 0;
        
        if (A[A.length-1] != A[A.length-2])
        {
        	return A[A.length-1];
        }
        
        for (int i = 0; i < A.length-1; i+=2)
        {
            if (A[i] != A[i+1])
            {
                result = A[i];
                break;
            }
        }
        
        return result;
    }

  • 0
    L

    Sort definitely needs extra space


Log in to reply
 

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