-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_duplicates.cpp
More file actions
59 lines (54 loc) · 1.41 KB
/
find_duplicates.cpp
File metadata and controls
59 lines (54 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* =====================================================================================
*
* Filename: find_duplicates.cpp
*
* Description: Find All Duplicates in an Array.
* Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some
* elements appear twice and others appear once. Find all the elements
* that appear twice in this array.
*
* Version: 1.0
* Created: 02/18/19 09:57:22
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <vector>
class Solution
{
public:
std::vector<int> findDuplicates(const std::vector<int>& nums)
{
std::vector<int> flags(nums.size(), 0);
for (auto n: nums)
{
flags[n - 1] += 1;
}
std::vector<int> dups;
for (int i = 0; i < flags.size(); i++)
{
if (flags[i] > 1)
{
dups.push_back(i + 1);
}
}
return dups;
}
};
int main(int argc, char* argv[])
{
std::vector<int> nums = {4, 3, 2, 7, 8, 2, 3, 1};
auto dups = Solution().findDuplicates(nums);
for (auto n: dups)
{
printf("%d ", n);
}
printf("\n");
return 0;
}