very simple O(n) C++ Code


  • 0
    V
    class Solution {
    public:
        vector<int> findDuplicates(vector<int>& nums) {
            int size = nums.size();
            vector<int> result;
            int temp[size+1] = { 0 };
            
            for (int i = 0; i < size; ++i)
            {
                if (++temp[nums[i]] == 2)
                {
                    result.push_back(nums[i]);
                }
            }
            
            return result;
        }
    };
    

Log in to reply
 

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