简体   繁体   中英

Finding the most frequent number in an array

I'm trying to see which number occurs the most in an array. I wrote this code, but its not outputting the correct numbers. Could someone tell me where I am going wrong

My logic:

  1. It goes through the array to find numbers that are the same to itself. Then it adds +1 to each time it finds a number that is the same to itself (loop_freq_amt).
  2. Then after it finishes the array it sees if the loop_freq_amt (the number's frequency it just checked) is greater than the current number which as the most frequency (freq_amt). If it is greater, then the freq_num gets replaced.

int frequency_number (int array[]) {
int freq_num = 0;

int i = 0;
int j = 0;
while(i < ARRAY_SIZE) {
    if (array[i] == array[j]) {
        freq_calcualtor = (freq_number);
    }

return freq_num;
}

You forgot to set freq_amt , so it is always 0. Every number seems more frequent than that, so you will get the last number instead of the most frequent one..

    if(loop_freq_amt > freq_amt) {
        freq_num = loop_freq_num;
        freq_amt = loop_freq_amt; // Added
    }

You have some little error in your code.

  1. You have "total_number" number in your array, so your loop need to go from 0 to total_number - 1.
  2. You forgot to update freq_amt when you have a new max frequencies number.

After, some coding style advice : when looping an array, use a for loop with inner iterator declaration. The variable loop_fre_num is useless.

int most_freq(int num_array[], size_t total_numbers)
{
    int freq_num = 0;
    int freq_amt = 0;

    for (size_t i = 0; i < total_numbers; ++i) {
        int loop_freq_amt = 0;

        for (size_t j = 0; j < total_numbers; ++j) {
            if (num_array[i] == num_array[j]) {
                ++loop_freq_amt;
            }
        }

        if(loop_freq_amt > freq_amt) {
            freq_num = num_array[i];
            freq_amt = loop_freq_amt;
        }
    }
    return freq_num;
}

About the "copy, sort and find the longuest sequence", here is an implementation :

int qsort_comp_int(const void *elem1, const void *elem2)
{
    return (*(int *)(elem1) - *(int *)(elem2));
}


int most_freq(int num_array[], size_t total_numbers)
{
    int    *copy            = NULL;
    size_t copyByteSize     = sizeof(*copy) * total_numbers;
    int    freq_num         = num_array[0];
    int    freq_max_amt     = 0;
    int    freq_current_amt = 0;

    copy = malloc(copyByteSize);

    memcpy(copy, num_array, copyByteSize);

    qsort(copy, total_numbers, sizeof(*copy), qsort_comp_int);


    for (size_t i = 0; i < total_numbers; ++i) {
        if (i != 0 && copy[i] != copy[i - 1]) {
            if (freq_max_amt < freq_current_amt) {
                freq_num = copy[i - 1];
                freq_max_amt = freq_current_amt;
            }
            freq_current_amt = 0;
        } else {
            ++freq_current_amt;
        }
    }

    free(copy);
    return (freq_num);
}

int main (void)
{
    int array[] = {1, 2, 3, 4, 1, 1, 1, 0, 4, 10, 3};

    printf("'%d'\n", most_freq(array, sizeof(array)/sizeof(*array)));

}

You forgot to update freq_amt.

An other solution is to create an accumulator (array with size of the max number) and in the first loop you increment the accumulator of the current number

And in the second loop find the value of the accumulator corresponding to the more freq number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM