Sort and find duplicates C++ solution


  • -1
    class Solution {
    public:
        vector<int> findDuplicates(vector<int>& nums) {
            vector<int> retVec;
            // there is no duplicates in vector(size < 2)
            if (2 > nums.size()) {
            	return retVec;
            }
    
            sort(nums.begin(), nums.end()); // sort vector
            for (int i = 0; i < nums.size() - 1; ++i) {
            	if (nums[i] == nums[i + 1]) {
            		retVec.push_back(nums[i]);
            	}
            }
            return retVec;
        }
    };
    

Log in to reply
 

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