There was an error while loading. Please reload this page.
1 parent 4b8d6d5 commit 5fda0c1Copy full SHA for 5fda0c1
week1/counting-elements.cpp
@@ -0,0 +1,21 @@
1
+ class Solution {
2
+public:
3
+ int countElements(vector<int>& arr) {
4
+ map<int, int> counter;
5
+ int totals = 0;
6
+
7
+ for (vector<int>::iterator it = arr.begin(); it != arr.end(); ++it) {
8
+ ++(counter[(*it)]);
9
+ }
10
11
+ // Get rid of dups
12
+ sort(arr.begin(), arr.end());
13
+ vector<int>::iterator newEnd = unique(arr.begin(), arr.end());
14
15
+ for (vector<int>::iterator it = arr.begin(); it != newEnd; ++it) {
16
+ totals += counter[(*it)-1];
17
18
19
+ return totals;
20
21
+};
0 commit comments