Last active
November 27, 2023 16:43
-
-
Save juanfal/079916adc1420f95017328d1f04797e7 to your computer and use it in GitHub Desktop.
Sieve a list picking up the elements repeated a certain number of times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sieveOfRepeated.cpp | |
// juanfc 2023-11-27 | |
// https://gist.github.com/079916adc1420f95017328d1f04797e7 | |
#include <iostream> | |
#include <array> | |
using namespace std; | |
const int MAX = 100; | |
struct TOpenAr { | |
int n; | |
array<int, MAX> ar; | |
}; | |
TOpenAr sieve(TOpenAr a, int reps); | |
void print(TOpenAr a); | |
int main() | |
{ | |
TOpenAr a = {9, {{1,3,4,3,1,3,0,-6,4}}}; | |
print(sieve(a, 2)); | |
a = (TOpenAr){5, {{1,2,3,4,5}}}; | |
print(sieve(a, 1)); | |
a = (TOpenAr){0, {{}}}; | |
print(sieve(a, 2)); | |
return 0; | |
} | |
int count(TOpenAr a, int x); | |
TOpenAr sieve(TOpenAr a, int reps) | |
{ | |
TOpenAr r; | |
r.n = 0; | |
for (int i = 0; i < a.n; ++i) | |
if (count(a, a.ar[i]) == reps and count(r, a.ar[i]) == 0) | |
r.ar[r.n++] = a.ar[i]; | |
return r; | |
} | |
int count(TOpenAr a, int x) | |
{ | |
int cnt = 0; | |
for (int i = 0; i < a.n; ++i) | |
if (a.ar[i] == x) | |
++cnt; | |
return cnt; | |
} | |
void print(TOpenAr a) | |
{ | |
for (int i = 0; i < a.n; ++i) | |
cout << a.ar[i] << " "; | |
cout << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment