Reapating numbers

Hello, I got a task to do in my university studies, and I don't have any idea how to do it,may somebody help me ? So the task is to make an array and make a program that checks if there is any numbers that repeats more then 2 times .

I got the basic code written here that I should be using :


Last edited on
The best way to deal with counting unknown items is to use map - so the key is the number and the associated data is the count.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <map>
#include <iostream>

int main()
{
	std::map<int, int> dups {};

	int n {};

	std::cout << "How many number: ";
	std::cin >> n;

	std::cout << "Enter numbers: ";

	for (int i = 0, j = 0; i < n && (std::cin >> j); ++i)
		++dups[j];

	for (const auto& [num, cnt] : dups)
		if (cnt > 2)
			std::cout << num << " occurred " << cnt << " times\n";
}



How many number: 10
Enter numbers: 1 1 1 1 2 2 2 3 3 3
1 occurred 4 times
2 occurred 3 times
3 occurred 3 times

Last edited on
If you can't use a map and need to use an array, then consider:

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
#include <iostream>
#include <algorithm>

int main()
{
	const size_t maxno {10};
	size_t n {};

	int nos[maxno] {};

	std::cout << "How many numbers (max " << maxno << "): ";
	std::cin >> n;

	n = std::min(n, maxno);

	std::cout << "Enter numbers: ";

	for (int i = 0, j = 0; i < n && (std::cin >> j); nos[i++] = j);

	std::sort(nos, nos + n);

	size_t cnt {1};
	int cur {nos[0]};

	for (int i = 1; i < n; ++i)
		if (nos[i] == cur)
			++cnt;
		else {
			if (cnt > 2)
				std::cout << cur << " occurs " << cnt << " times\n";

			cnt = 1;
			cur = nos[i];
		}

	if (cnt > 2)
		std::cout << cur << " occurs " << cnt << " times\n";
}

Last edited on
neonblue00700 wrote:
So the task is to make an array and make a program that checks if there is any numbers that repeats more then 2 times

Please clarify. Do you want an answer of simply "true" or "false" or do you want to output those particular numbers. Or do you want to output how many numbers fall into that category.

What precisely does "repeats more than 2 times" mean? Does it mean at least 2 or does it mean at least 3 or does it mean at least 4 occurrences of the same number?

On the whole it is usually best to post your original assignment verbatim.

If you only need whether any numbers repeat then there are far faster means of doing it.
I need to find if in a random array a number repeats more then 2 times , and if there is a number like that, cout<<"Yes" , if not cout <<"NO";
That's one part of the answer - how about the rest?

In this sequence 2 repeats once: 2 2
In this sequence 2 repeats twice: 2 2 2
In this sequence 2 repeats more than twice: 2 2 2 2

So, exactly how many occurrences of a number do you want before you flag it?

Oh, and I did write:
On the whole it is usually best to post your original assignment verbatim.
Last edited on
Topic archived. No new replies allowed.