C++ solution with HashTable


  • 0
    M

    class Solution {
    public:
    int singleNumber(vector<int>& nums) {
    unordered_set<int> map;
    for(int i = 0; i<nums.size(); i++){
    // iterator
    // if we can not find this value in the hash table,
    // push this value into the hash table
    if(map.find(nums[i]) == map.end()){
    map.insert(nums[i]);
    }
    else if(map.find(nums[i]) != map.end()){
    map.erase(nums[i]);
    }
    }
    return *map.begin();
    }
    };


Log in to reply
 

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