# Thread: Counting Occurrance of Numbers

1. ## Counting Occurrance of Numbers

Can anyone explain to me how to count the number of occurrance of any number? Say for example I inputed this into my program:

1 -3 -3 1 2 4 6 1

Then my program should print this with the result in descending order:

6 occurs 1
4 occurs 1
2 occurs 1
1 occurs 3
-3 occurs 2

Any ideas?

2. Store the numbers in an array. Create a linked list, consisting of structs which contain the a number and the frequency of the number in the array, and ofcourse a pointer to the next node in the list.

There are several strategies to creating the list:

1. Read a number of the array, check if it occurs in the linked list. If yes, then increase frequency, else create a new node.

2. Read a number of the array, count how many times it occurs in the array and create a node for the number.

Finally sort the list based on frequency and print it out.

3. I'm only up to pointers and arrays in my book, so this problem can be solved w/o link lists.

4. In that case you could use a 2D-array instead of the linked list. In the first column store the number and in the second the frequency.

5. Originally posted by Shiro
In that case you could use a 2D-array instead of the linked list. In the first column store the number and in the second the frequency.
Yeah I thought of a 2D array too but it doesn't work. If I input these numbers:

1 2 3 4 1 2 3 4

Then my array should be 2 (rows) by 8 (columes). I test the first element (the number 1) to see if it occurs again (only one more time, so 2 in total); so a[0][0] has the value 1, and a[1][0] should have the value 2. But later when my program goes to the 5th element of my array (the number 1 again) it'll do the counting again and assign a[4][1] the value 1. I'm not sure how to solve this.

6. The problem is that you have the source and destination arrays being the same. To solve this problem you could use two arrays, one for the source and one for the destination. To give an impression of what I mean, here an example:

#define NR_OF_INPUTS 8

int source [NR_OF_INPUTS];
int dest [NR_OF_INPUTS][NR_OF_INPUTS];

Let source contain the following elements:

source = [1, 2, 3, 7, 2, 1, 2, 5]

Then the dest array would have the following elements:

dest = [(1, 2), (2, 3), (3, 1), (7, 1), (5, 1)]

In C it would look like this:

dest [0][0] = 1; /* The number */
dest [0][1] = 2; /* It's frequency in source */
dest [1][0] = 2;
dest [1][1] = 3;
etc.

So first fill the source-array. And then fill the dest-array. You could start with the first element and if you get to an element which already exists in the dest-array, you can just increment the frequency by one. Finally you can sort the dest-array by sorting the second column, which contains the frequencies. And don't forget to replace both number and frequency when replacements need to be done.

7. Originally posted by Salem
Given that your output seems to be sorted, I would suggest that sorting the array before counting would be the easy thing to do

If you know it's sorted, you only have to spot when the number changes. If you see a 6 followed by 4, you know there are no more 6's in the array, and thus you can just print how many you've seen so far.
Good point