Very simple C++ solution with `set`


  • 0
    Y
    class Solution {
    public:
        vector<int> findDuplicates(vector<int>& nums) {
            vector<int> result;
            set<int> dup;
            for(int i=0;i<nums.size();i++){
                if(dup.count(nums[i])!=0)
                    result.push_back(nums[i]);
                else
                    dup.insert(nums[i]);
            }
            return result;
        }
    };
    

Log in to reply
 

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