Skip to content

Commit 5fda0c1

Browse files
counting numbers
1 parent 4b8d6d5 commit 5fda0c1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

week1/counting-elements.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)