### Thread: Comparing number... HELP @_@

1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2003
Posts
9
Rep Power
0

#### Comparing number... HELP @_@

I have a set of numbers & character, and each number/char appears more than once. Suppose: A, B, 6, 9, B, 8, 3, B……. (a total of 10 different kinds) How should I program, in order to find out the most occurring number/char? All the number/char has already been count, which means that I already know 6 appears six times, 9 appears 3 times, etc. I was trying to use a loop that save the first number as “temp” then start to compare the other, and if the temp is great, I would keep comparing until either the count of the other number/char is the same, then I would do something…but I start to get into infinite loop and very very long codes… any idea how to make it easier?
**Although this is a program in assembly language, I just want to get the concept (even in C is fine...)…

THANKS!
2. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
13
this is probably a very ineficiant way, but it's a way.

make an array whose index values cover all possible ascii character values that you may encounter and therefore need to count. fill all the elements with 0. then step through your text and use the character's ascii numeric value to increment the corresponding element in the array. you end up with an array with all the characters (including commas and spaces) counted.

Code:
```#include <stdio.h>
#define SIZE 128

main()
{
int i;
char chars[] = "A, B, 6, 9, B, 8, 3, B";
int counts[SIZE]	 = {0};
char *p = chars;

while(*p)
counts[*p++]++;

for(i = 0; i < SIZE; i++)
if(counts[i])
printf("%c %d\n", i, counts[i]);
}```
outputs:
Code:
```  7
, 7
3 1
6 1
8 1
9 1
A 1
B 3```