title: Most frequent element in a sequence using arrays in C
body: <p>I'm doing an online course on &quot;Programming, Data Structure &amp; Algorithm&quot;. I've been given an assignment to &quot;find the most frequent element in a sequence using arrays in C (with some constraints)&quot;. They've also provided some test-cases to verify the correctness of the program. But I think I'm wrong somewhere.</p>
  <p>Here's the complete question from my online course.</p>
  <blockquote>
  <p>INPUT</p>
  <p>Input contains two lines. First line in the input indicates N,
  the number of integers in the sequence. Second line contains N
  integers, separated by white space.</p>
  <p>OUTPUT</p>
  <p>Element with the maximum frequency. If two numbers have the
  same highest frequency, print the number that appears first in the
  sequence.</p>
  <p>CONSTRAINTS</p>
  <p>1 &lt;= N &lt;= 10000</p>
  <p>The integers will be in the range</p>
  <p>[-100,100].</p>
  </blockquote>
  <p>And here's the test cases.</p>
  <p>Test Case 1</p>
  <p>Input:</p>
  <pre><code>5
  1 2 1 3 1
  </code></pre>
  <p>Output:</p>
  <pre><code>1
  </code></pre>
  <p>Input:</p>
  <pre><code>6
  7 7 -2 3 1 1
  </code></pre>
  <p>Output:</p>
  <pre><code>7
  </code></pre>
  <p>And here's the code that I've written.</p>
  <pre><code>#include&lt;stdio.h&gt;
  
  int main()
  {
      int counter[201] = {0}, n, i, input, maximum = 0;
      scanf(&quot;%d&quot;, &amp;n);
      for(i = 1; i &lt;= n; i++) {
          scanf(&quot;%d&quot;, &amp;input);
          if(input &lt; -100 &amp;&amp; input &lt; 100)
              ++counter[input];
      }
      maximum = counter[0];
      for (i = 1; i &lt; 201; i++) {
          if (counter[i] &gt; maximum) {
              maximum = counter[i];
          }
      }
      printf(&quot;%d&quot;, maximum);
  
      return 0;
  }
  </code></pre>
  <p>Please tell me where I'm wrong. Thank you.</p>
  <p>EDIT:</p>
  <p>I've modified the code, as suggested by @zoska. Here's the working code.</p>
  <pre><code>#include&lt;stdio.h&gt;
  
  int main()
  {
      int counter[201] = {0}, n, i, input, maximum = 0;
      scanf(&quot;%d&quot;, &amp;n);
      for(i = 1; i &lt;= n; i++) {
          scanf(&quot;%d&quot;, &amp;input);
          if(input &lt; 100 &amp;&amp; input &gt; 0)
              ++counter[input + 100];
          else
              ++counter[input];
      }
      maximum = counter[0];
      for (i = 0; i &lt; 201; i++) {
          if (counter[i] &gt; maximum) {
              maximum = i - 100;
          }
      }
      printf(&quot;%d&quot;, maximum);
  
      return 0;
  }
  </code></pre>