My accepted answer for c++


  • 3
    D
    class Solution {
    public:
        int singleNumber(int A[], int n) {
            unordered_map<int, int> hash;
            for (int i = 0; i < n; i++)
            {
                if (hash.find(A[i]) != hash.end())
                {
                    hash.erase(A[i]);
                }else{
                    hash[A[i]] = A[i];
                }   
            }
            return hash.begin()->first;   
        }
    };

  • 0
    U

    The time complexity of hash.erase() is O(n), so the time complexity of your program is not linear


Log in to reply
 

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