Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. int mostCommon(int *input, int inLength, int upperLimit, int *outIndex, int *outCount) {
  2.     upperLimit++; //behöver ha index till och MED upperLimit
  3.  
  4.     int occurences[upperLimit];
  5.     int indexes[upperLimit];
  6.  
  7.     memset(occurences, 0, upperLimit * sizeof(int)); //alla index = 0
  8.     memset(indexes, -1, upperLimit * sizeof(int)); //alla index = -1
  9.  
  10.     for(int i = 0; i < inLength; i++) {
  11.         int number = input[i];
  12.         occurences[number]++;
  13.  
  14.         if(indexes[number] < 0) {
  15.             indexes[number] = i;
  16.         }
  17.     }
  18.  
  19.     int number = -1;
  20.     int count = -1;
  21.     int index = -1;
  22.     for(int i = 0; i < upperLimit; i++) {
  23.         if(occurences[i] > count) {
  24.             count = occurences[i];
  25.             index = indexes[i];
  26.             number = i;
  27.         }
  28.     }
  29.  
  30.     (*outIndex) = index;
  31.     (*outCount) = count;
  32.     return number;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement