So I'm trying to count the number of occurrences of each digit within an array.

My code I've got so far looks like the following:

``````#include <stdio.h>
#include <string.h>

int main()
{
int numbers = {1, 4, 5, 5, 5, 6, 6, 3, 2, 1};
int count = 0;

for(int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (numbers[i] == numbers[j])
{
count++;
}
}
printf("Number %d has occured %d many times \n", numbers[i], count);
count = 0;

}
}
``````

Only the output I get is the following:

``````Number: 1       Occurence: 2
Number: 4       Occurence: 1
Number: 5       Occurence: 3
Number: 5       Occurence: 3
Number: 5       Occurence: 3
Number: 6       Occurence: 2
Number: 6       Occurence: 2
Number: 3       Occurence: 1
Number: 2       Occurence: 1
Number: 1       Occurence: 2
``````

I only want to count the occurrence of EACH digit, it seems to be counting duplicates.

How can I correct this code? Can someone point me in the right direction.

Many thanks

Alonso

## All 3 Replies

I would have to say that you need a flagging system which marks a integer element when its been counted.

Are you sure this is C?

``````for(int i = 0; i < 10; i++)
{
...
}
``````

Yes, this is C. Changing the outer for loop to be < 10 counts each integer element. Thanks very much.

Keep a frequency table:

``````int numbers[] = {1, 4, 5, 5, 5, 6, 6, 3, 2, 1};
int freq = {0};

for (int i = 0; i < sizeof numbers / sizeof *numbers; i++) {
if (numbers[i] >= 0 && numbers[i] < 10) {
++freq[numbers[i]];
}
else {
printf("Invalid digit: %d\n", numbers[i]);
}
}

for (int i = 0; i < 10; i++) {
printf("%d has occured %d time(s)\n", i, freq[i]);
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8270842,"math_prob":0.9948908,"size":886,"snap":"2022-27-2022-33","text_gpt3_token_len":303,"char_repetition_ratio":0.2154195,"word_repetition_ratio":0.065476194,"special_character_ratio":0.35665914,"punctuation_ratio":0.23004694,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97270113,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-14T07:27:36Z\",\"WARC-Record-ID\":\"<urn:uuid:a75c000e-2f24-4341-8b25-c6934e61a787>\",\"Content-Length\":\"76117\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7cfa98ea-3ff7-4fab-a9c7-f519e6af84dc>\",\"WARC-Concurrent-To\":\"<urn:uuid:363e6b05-abb2-4578-b9b1-f6307a714edc>\",\"WARC-IP-Address\":\"172.66.41.5\",\"WARC-Target-URI\":\"https://www.daniweb.com/programming/software-development/threads/469103/count-number-of-occurrences-of-a-digit-within-a-string\",\"WARC-Payload-Digest\":\"sha1:MHAK4UK6EKCNP5RMLG3XED64APKXN53H\",\"WARC-Block-Digest\":\"sha1:QDXTRKAORVEJWPFUV7YXR4DQCSCOYTXV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571996.63_warc_CC-MAIN-20220814052950-20220814082950-00167.warc.gz\"}"}