title: If only one element in a hashset, how can I get it out?
body: <pre><code>HashSet&lt;Integer&gt; set = new HashSet&lt;Integer&gt;();
  set.add(1);
  </code></pre>
  
  <p>How can I get the <code>1</code> out? I can do it by <code>for(integer i : set)</code>. Do you have another idea to solve the problem?</p>
  
  <p>My specified problem is "Given an array of integers, every element appears twice except for one. Find that single one."
  I want to use add elements into set if the set doesn't contain it and remove existed element during loop. And the last remain element is answer. I don't know how to return it.</p>
  
  <pre><code>    public static int singleNumber(int[] A) {
      HashSet&lt;Integer&gt; set = new HashSet&lt;Integer&gt;();
      for (int a : A) {
          if (!set.contains(a)) {
              set.add(a);
          } else {
              set.remove(a);
          }
      }
      /**
       * for(Integer i : set) { return i; }
       *return A[0];//need one useless return
      /**
       * while(set.iterator().hasNext()) { return set.iterator().next(); }
       * return A[0];//need one useless return
       */
      return set.toArray(new Integer[1])[0];
  }
  </code></pre>