title: Efficient way to count unique pairs in int array
body: <p>This is my first post, hope it complies with posting guidelines of the site.
  First of all a generic thanks to all the community: reading you from some months and learned a lot :o)</p>
  
  <p>Premise: I'm a first years student of IT.</p>
  
  <p>Here's the question: I'm looking for an efficient way to count the number of unique pairs (numbers that appear exactly twice) in a given positive int array (that's all I know), e.g. if:</p>
  
  <pre><code>int[] arr = {1,4,7,1,5,7,4,1,5};
  </code></pre>
  
  <p>the number of unique pairs in arr is 3 (4,5,7).</p>
  
  <p>I have some difficulties in... evaluating the efficiency of my proposals let's say.</p>
  
  <p>Here's the first code I did:</p>
  
  <pre><code>int numCouples( int[] v ) {
  int res = 0;
  int count = 0;
  for (int i = 0 ; i &lt; v.length; i++){
      count = 0;
      for (int j = 0; j &lt; v.length; j++){
          if (i != j &amp;&amp; v[i] == v[j]){
              count++;
          }
      }
      if (count == 1){
          res++;
      }
  }
  return res/2;
  }
  </code></pre>
  
  <p>This shoudn't be good cause it checks the whole given array as many times as the number of elements in the given array... correct me if I'm wrong.</p>
  
  <p>This is my second code:</p>
  
  <pre><code>int numCouples( int[] v) {
  int n = 0;
  int res = 0;
  for (int i = 0; i &lt; v.length; i++){
      if (v[i] &gt; n){
          n = v[i];
      }
  }
  int[] a = new int [n];
  for (int i = 0; i &lt; v.length; i++){
      a[v[i]-1]++;
  }
  for (int i = 0; i &lt; a.length; i++){
      if (a[i] == 2){
          res++;
      }
  }
  return res;
  }
  </code></pre>
  
  <p>I guess this should be better than the first one since it checks only 2 times the given array and 1 time the n array, when n is the max value of the given array. May be not so good if n is quite big I guess...</p>
  
  <p>Well, 2 questions:</p>
  
  <ol>
  <li><p>am I understanding good how to "measure" the efficiency of the code?</p></li>
  <li><p>there's a better way to count the number of unique pairs in a given array?</p></li>
  </ol>
  
  <p>EDIT:
  Damn I've just posted and I'm already swamped by answers! Thanks! I'll study each one with care, for the time being I say I don't get those involving HashMap: out of my knowledge yet (hence thanks again for the insight:o) )</p>