Skip to content

Commit 0f9d502

Browse files
authored
Merge pull request #225 from majvax/snippets-c++
[Snippets-C++] Added two snippets from #209
2 parents 36b129a + da7b75c commit 0f9d502

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

snippets/cpp/array-manipulation/filter-vector.md renamed to snippets/cpp/vector-manipulation.md/filter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Title: Filter Vector
2+
Title: Filter
33
Description: Filters a vector using a predicate function.
44
Author: majvax
55
Tags: array,filter,c++23
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: Remove duplicates
3+
description: Removes duplicates from an vector of ints
4+
author: AnkushRoy-code
5+
tags: vector,remove,duplicate
6+
contributor: majvax
7+
---
8+
9+
```cpp
10+
#include <algorithm>
11+
#include <vector>
12+
13+
bool removeDuplicates(std::vector<int> &input) noexcept {
14+
if (input.empty()) return false;
15+
const auto size = input.size();
16+
std::sort(input.begin(), input.end());
17+
18+
auto last = std::unique(input.begin(), input.end()); // remove duplicates
19+
input.erase(last, input.end()); // resize vector and delete the undefined elements
20+
21+
return size != input.size();
22+
}
23+
24+
25+
26+
// Usage:
27+
std::vector<int> vec = {4, 2, 2, 8, 5, 6, 9, 9, 9, 8, 8, 4};
28+
removeDuplicates(vec); // returns {2, 4, 5, 6, 8, 9}
29+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: Remove n Occurences
3+
description: Removes duplicates from an vector of ints
4+
author: AnkushRoy-code
5+
tags: vector,remove
6+
contributor: majvax
7+
---
8+
9+
```cpp
10+
#include <algorithm>
11+
#include <unordered_map>
12+
#include <vector>
13+
14+
bool removeOccurrences(std::vector<int>& vec, const unsigned int n) noexcept {
15+
if (vec.empty() || n == 0) return false;
16+
17+
const auto size = vec.size();
18+
std::unordered_map<int, int> frequency; // count frequencies
19+
for (int num : vec) {
20+
frequency[num]++;
21+
}
22+
23+
vec.erase( // remove elements with n number of occurrences
24+
std::remove_if(vec.begin(), vec.end(), [&frequency, n](const int x) {
25+
return frequency[x] == n;
26+
}),
27+
vec.end()
28+
);
29+
return size != vec.size();
30+
}
31+
32+
33+
34+
// Usage:
35+
std::vector<int> vec = {4, 2, 4, 8, 5, 6, 8, 8, 4, 3 };
36+
37+
int n = 3; // Remove elements that occur exactly 3 times
38+
removeOccurrences(vec, n); // returns {2, 5, 6, 3}
39+
```

snippets/cpp/array-manipulation/transform-vector.md renamed to snippets/cpp/vector-manipulation.md/transform.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Title: Transform Vector
2+
Title: Transform
33
Description: Transforms a vector using a function.
44
Author: majvax
55
Tags: array,transform,c++23

0 commit comments

Comments
 (0)