Help calculating max amt of times a number appears in an array

Hi! So I'm a little lost right now, and am wondering if anyone had any tips for my homework.
So what I need to do is print out the number that appears most in the array. It's supposed to be out of 10,000 numbers, but for now I'm doing 10 because it seems easier to print and test out and I can change the numbers later.
As I currently understand (and wrote out but still a little confused), I should go through the array one value at a time, and go all the way through and increment a count variable every time said number appears. However, I'm a little confused how I should continue... Its not properly counting the max amount of times a number appears (It should only print the # with the highest occurences btw, not all of them - Idk how to do that either.)

Thank you!

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 #include<iostream>
#include<cstdlib>
#include<iomanip>
#include<ctime>
#include<cmath>

using namespace std;

void sortArray(int bigArray[], int SIZE);


int main()

{
	srand(time(0));

	const int SIZE = 25;
	int bigArray[SIZE];
	int arrayCount[100] = {0};
	int maxCount = 1;
	int num = 0;

	for (int i = 0; i < SIZE; i++)
	{
		int randomNum = (rand() % 100) + 1;
		bigArray[i] = randomNum;
	}

	sortArray(bigArray,SIZE);

	for (int i = 0; i < 10; i++)
	{
		num = bigArray[i];
		arrayCount[num]++;
	}
	
	for (int i = 0; i < SIZE; i++)
	{
		cout << bigArray[i] << endl;
	}
	
    for (int i = 0; i < SIZE; i++)
    {
        if (bigArray[i] == bigArray[i+1])
        {
            maxCount++;   
        }
        else if (bigArray[i] != bigArray[i+1])
        {
            cout << bigArray[i] << " appeared " << maxCount << " times." << endl; 
            maxCount = 1;
        }
    }

	
	system("pause");
}

void sortArray(int bigArray[], int SIZE)
{
	int temp = 0;

	for (int i = 0; i < SIZE; i++)
	{
		for (int j = 0; j < SIZE; j++)
		{
			if (bigArray[j] > bigArray[j+1])
			{
				temp = bigArray[j];
				bigArray[j] = bigArray[j+1];
				bigArray[j+1] = temp;
			}

		}
	}
}
Do we need to use dynamic memory allocation?
Static memory allocation looks convenient but may potentially cause stack overflow.
Static memory,
I haven't learned pointers yet
Why do you need to sort array?
You only need to find the Max element.
Last edited on
I thought it would be the best solution because I also have to find the median value
Why do we need to sort array?
You only need to find the Max element.


Nowhere in this thread or its title is the goal specified as "find the Max element."

To the OP: You need to keep track of the element value which is the element with the highest element count and its count, as well as the count of the current run. You cannot keep track of two counts with one variable. You also need to avoid indexing bigArray with invalid indexes.




The title is what that makes it confusing.
Max amt of times a number


What exactly does this phrase mean?
Sorry it was vague I was in a rush, it means the maximum times a number appears - so if 40 appears the most, I'd print that 40 appeared the most and how many times it took to appear.


And which index are you referring to Cire? A little confused here! X:
And which index are you referring to Cire? A little confused here! X:


1
2
3
4
5
6
7
8
9
10
11
12
    for (int i = 0; i < SIZE; i++)
    {
        if (bigArray[i] == bigArray[i+1])
        {
            maxCount++;   
        }
        else if (bigArray[i] != bigArray[i+1])
        {
            cout << bigArray[i] << " appeared " << maxCount << " times." << endl; 
            maxCount = 1;
        }
    }


Since i can be SIZE-1, i+1 can be an invalid index.

So I noticed that code wasn't running - I don't think I should even have it.


Would I be able to create the random numbers AND store the count in the initial for loop at the same time? Would that be a viable solution?

edit: by that code I mean't the one with bigArray
Last edited on
closed account (LA48b7Xj)
Using a std::map or std::unordered_map is a way to keep track of the occurrence of things.

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

using namespace std;

int main()
{
    array<int, 100> numbers;
    unordered_map<int, int> num_count;

    for(auto& e : numbers)
    {
        e = rand()%10 + 1;
        cout << e << ' ';
        ++num_count[e];
    }

    cout << '\n';

    auto max_count = *std::max_element(num_count.begin(), num_count.end(),
        [](const pair<int, int>& p1, const pair<int, int>& p2)
        { return p1.second < p2.second; });

    cout << max_count.first << " appeared the most: " << max_count.second << " times" << endl;
}
Last edited on
Topic archived. No new replies allowed.