title: Duplicate numbers in an array
body: <p>I need to write a program where I have to print the numbers which occur twice or more than twice in the array. To make things simpler, I am working on a sorted array.</p>
  
  <p>Here is my code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main()
  {
      int n,i;
      int a[10]={2,2,2,4,6,6,9,10,10,11};
      printf("%d\n",a[10]);
      for(i=0;i&lt;10;++i)
      {
          if(a[i]==a[i+1] &amp;&amp; a[i]!=a[i-1])
              printf("%d ",a[i]);
          else
              continue;
      }
      return 0;
  }
  </code></pre>
  
  <p>The code seems to work fine but I do not like my code because at some point, the loop compares the value of a[0] with a[-1] and a[9] with a[10] and both of these, a[-1] and a[10], are garbage values. I am sure there are better ways to do it but I am unable to think of any.</p>
  
  <p>Also, I need to extend the above program to count the frequency of duplicate numbers.</p>
  
  <p>Help is appreciated. Thanks!</p>