title: Find max/min occurrence in integer array
body: <p>I just finished writing an algorithm that finds values in an input integer array with max/min occurrences. My idea is to sort the array (all the occurrences are now in sequence) and use a <code>&lt;value:occurrences&gt;</code> pair to store for every value the number of occurrences correspondent. </p>
  
  <p>It should be <code>O(nlogn)</code> complexity but I think that there are some constant multipliers. What can I do to improve performance?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "e7_8.h"
  
  #define N 20
  /*Structure for &lt;value, frequencies_count&gt; pair*/
  typedef struct {
      int value;
      int freq;
  } VAL_FREQ;
  
  
  void  get_freq(int *v, int n, int *most_freq, int *less_freq) {
  
      int v_i, vf_i, current_value, current_freq;
  
      VAL_FREQ* sp = malloc(n*sizeof(VAL_FREQ));
      if(sp == NULL) exit(EXIT_FAILURE);
  
      mergesort(v,n);
  
      vf_i = 0;
      current_value = v[0];
      current_freq = 1;
      for(v_i=1; v_i&lt;n+1; v_i++) {
          if(v[v_i] == current_value) current_freq++;
          else{
              sp[vf_i].value = current_value;
              sp[vf_i++].freq = current_freq;
              current_value = v[v_i];
              current_freq = 1;
          }
      }
      /*Finding max,min frequency*/
      int i, max_freq_val, max_freq, min_freq_val, min_freq;
  
      max_freq = sp[0].freq;
      max_freq_val = sp[0].value;
      min_freq = sp[0].freq;
      min_freq_val = sp[0].value;
      for(i=1; i&lt;vf_i; i++) {
          if(sp[i].freq &gt; max_freq) {
              max_freq = sp[i].freq;
              max_freq_val = sp[i].value;
          }
          if(sp[i].freq &lt; min_freq) {
              min_freq = sp[i].freq;
              min_freq_val = sp[i].value;
          }
      }
  
      *most_freq = max_freq_val;
      *less_freq = min_freq_val;
  
      free(sp);
  }
  </code></pre>