Basic 48ms C++ solution with unordered_map


  • 16
    D
    class Solution {
    public:
        bool containsDuplicate(vector<int>& nums) {
            if (nums.empty()) { return false; }
            unordered_map<int,int> mp;
            for (int i : nums) {
                if (++mp[i] > 1) {
                    return true;
                }
            }
            return false;
        }
    };

  • 0
    L

    could u explain a little for this code?


  • 0
    G
    This post is deleted!

  • 1
    G

    Iterate nums, if i in nums is not the key of mp, add it to the mp, and ++mp[i] so the mp[i] is 1 now, compare mp[i] with 1, if i is duplicated, then mp[i] is larger than 1, return true.


  • 0
    T

    ++mp[i] > 1
    awesome ~


Log in to reply
 

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