Commit b33e2d55d4c0e63bbb07289291a1841a365daf36
Committed by
Ronaldo
1 parent
76e94dfea8
Exists in
main
Create directory Unordered_Set and file Duplicates.cpp.
Showing 2 changed files with 43 additions and 1 deletions Side-by-side Diff
SearchingSortingandBasicDataStructures/InbuiltSorting/Problem5/SimpleTask.cpp
View file @
b33e2d5
SearchingSortingandBasicDataStructures/Unordered_Set/Duplicates.cpp
View file @
b33e2d5
1 | +// | |
2 | +// Created by ronal on 2/26/2023. | |
3 | +// Problem Link Description | |
4 | +// https://leetcode.com/problems/find-all-duplicates-in-an-array/ | |
5 | + | |
6 | +#include <bits/stdc++.h> | |
7 | + | |
8 | +using namespace std; | |
9 | +class Solution { | |
10 | +public: | |
11 | + vector<int> findDuplicates(vector<int>& nums) { | |
12 | + vector<int> duplicates; | |
13 | + unordered_set<int> inSet; | |
14 | + unordered_set<int> duplicatesSet; | |
15 | + | |
16 | + for(auto value : nums){ | |
17 | + if(inSet.find(value) == inSet.end()){ | |
18 | + inSet.insert(value); | |
19 | + }else{ | |
20 | + duplicatesSet.insert(value); | |
21 | + } | |
22 | + } | |
23 | + | |
24 | + for(auto value : duplicatesSet){ | |
25 | + duplicates.push_back(value); | |
26 | + } | |
27 | + return duplicates; | |
28 | + } | |
29 | +}; | |
30 | + | |
31 | + | |
32 | +int main(){ | |
33 | + Solution solution; | |
34 | + vector<int> arr = {4,3,2,7,8,2,3,1}; | |
35 | + vector<int> duplicates = solution.findDuplicates(arr); | |
36 | + for(auto value : duplicates){ | |
37 | + cout << value << " "; | |
38 | + } | |
39 | + cout << "\n"; | |
40 | + return 0; | |
41 | +} |