title: Efficient algorithm for counting number of pairs of identical elements in an array
body: <p>For example,  </p>
  
  <pre><code>int num[] = {1, 5, 3, 12, 5, 1, 4};
  int len = 7;
  int count = 0;
  </code></pre>
  
  <p>(Assuming there are no more than 2 of identical elements in the array)<br>
  Then I would do  </p>
  
  <pre><code>for(int i=0; i&lt;len-1; i++) {
    for(int j=i+1; j&lt;len; j++) {
      if(num[i] == num[j]) {
        count++;
      }
    }
  }  
  </code></pre>
  
  <p>Then count would be 2.</p>
  
  <p>But this algorithm results in the efficiency of O(N^2).<br>
  Is there a more efficient way?<br>
  Thank you in advance!</p>