Find total number of frequently occurrence elements

78 views Asked by At

I was recently working on group of numbers, i have a large sequence of numbers stored vertically. i was counting which number how many times appears, and then i chose the number(s) with high hit counts.

my code looks like this:

 // select the most repeated number 
 int countArray[1000];
    seq_len = i;
    for (i=0; i < seq_len; i++) {
            scanf("%d", page_seq[i]);
            countArray[i] = -1; 
 }

    for(i=0; i < seq_len; i++) {
            count = 1;
            for( j = i+1; j < seq_len; j++) {
                    if(page_seq[i] == page_seq[j]) {
                            countArray[j] = 0;
                            count++;
                    }
            }
            if(countArray[i] != 0) {
                    countArray[i] = count;
            }
    }
     //print count of each page frequency
    for(i=0; i < seq_len; i++) {
            if(countArray[i] != 0) {
            printf("page %d : count %d\n", page_seq[i], countArray[i]);
            }
    }

      //distinguish most frequently readed page and print its read count

     int most_freq;
 int maxPage=-1, maxPageIdx=-1;
 int pinned_page;
    for(i=0; i < seq_len; i++) {
            if(countArray[i] != 0) {
            if(page_seq[i] > maxPage) {
                    maxPage = page_seq[i];
                    maxPageIdx = i;
            }
    }
    for(i=0; i < seq_len; ++i) {
            if(countArray[0] < countArray[i])
                    countArray[0] = countArray[i];
             scanf("%d",countArray[i]);
            most_freq = countArray[0];
    }
    for (i =0; i< seq_len; i++) {
            if(countArray[i]  > most_freq){
                    most_freq = countArray[i];
                    count++;
            }
    }
    }
    pinned_page = page_seq[maxPage];
    printf("pinned page %d\n", pinned_page);

it works with simple (1,2,3,4) numbers,and i get my each page frequency count and i see highest counted number but with in ex. 139595776 139538432 139534336 139632640 these numbers it doesn't show me the highest counted number, instead im getting SEGMENTATION FAULT(core dumped)

any help will be appreciated

1

There are 1 answers

0
Urudin On

You may have problems with big numbers, because you are using int. Basic signed integer type. Capable of containing at least the [−32,767, +32,767] range;[3][4] thus, it is at least 16 bits in size. Try to work with long instead of int, maybe that helps on your problem.