Easy implementation using unordered_map


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

  • 0
    S

    the question said can't use extra space, why you code can be accepted?


  • 0
    J

    “Could you implement it without using extra memory?”
    not "you must implement it without using extra memory"

    but we do have other solutions that using no extra space


Log in to reply
 

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