My simple C++ solution using Unordered Map


  • 0
    R
    class Solution {
    public:
        int findDuplicate(vector<int>& nums) {
            unordered_map<int, int>m;
            for(int i=0; i<nums.size();i++) {
                m[nums[i]]++;
                if(m[nums[i]]>1) return nums[i];
            }
            return 0;
        }
    };
    

Log in to reply
 

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