title: Finding number of repeated elements in an array using C
body: <p>I am unable to find the correct logic to find the number of repeated elements in an array. I can understand why my logic is not working, but I am not able to overcome it.</p>
  <p>Following is the actual question:</p>
  <blockquote>
  <p>Write a program that declares an integer array <code>arr</code> of
  size <code>n</code>. It first takes in a positive integer <code>n</code> from the user. Then reads <code>n</code> numbers
  and stores them in <code>arr</code>. It checks and prints the number of repetitions in <code>arr</code>.
  The result is the sum of the total number of repetitions in the array.<br />
  Example: If <code>arr</code> = <code>[4,3,4,4,3,4,5]</code><br />
  Then number of repetitions is <code>6</code> (which is 4 repetitions of <code>4</code> +
  2 repetitions of <code>3</code>)</p>
  </blockquote>
  <p>Following is my code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  int main() {
      int n, i, j, count = 1, p = 0;
      printf(&quot;Enter the length of array&quot;);
      scanf(&quot;%d&quot;, &amp;n);
      int arr[n];
      for (i = 0; i &lt; n; i++) {
          printf(&quot;Enter a number\n&quot;);
          scanf(&quot;%d&quot;, &amp;arr[i]);
      }
      for (i = 0; i &lt; n; i++) {
          for (j = i + 1; j &lt; n; j++) {
              if (arr[i] == arr[j]) {
                  count++;
                  break;
              }
          }
          if (count != 1) {
              p = p + count;
              count = 1;
          }     
      }
      printf(&quot;Number of repetitions are %d&quot;, p);
  }
  </code></pre>
  <p>For the above code if we take the array as mentioned in the question, then each time my code encounters two same 4's, it counts both of them, hence my program ends up counting extra number of 4's. So I am unable to find some better logic to over come it.<br />
  (<em>I am a beginner so  I doesn't no much advanced function/methods used in C</em>)</p>