title: What is wrong with my Java solution to Codility MissingInteger?
body: <p>I am trying to solve the codility MissingInteger problem <a href="https://codility.com/c/intro/demoZKF8W2-4TC" rel="nofollow noreferrer">link</a>:</p>
  
  <blockquote>
    <p>Write a function:</p>
  
  <pre><code>class Solution { public int solution(int[] A); }
  </code></pre>
    
    <p>that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer that does not occur in A.
        For example, given:</p>
  
  <pre><code> A[0] = 1    
   A[1] = 3    
   A[2] = 6
   A[3] = 4    
   A[4] = 1    
   A[5] = 2
  </code></pre>
    
    <p>the function should return 5.</p>
    
    <p>Assume that:</p>
    
    <p>N is an integer within the range [1..100,000];
        each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].</p>
    
    <p>Complexity:</p>
    
    <p>expected worst-case time complexity is O(N);
        expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
        Elements of input arrays can be modified.</p>
  </blockquote>
  
  <p>My solution is:</p>
  
  <pre><code>class Solution {
      TreeMap&lt;Integer,Object&gt; all = new TreeMap&lt;Integer,Object&gt;();
  
      public int solution(int[] A) {
  
          for(int i=0; i&lt;A.length; i++)
              all.put(i+1,new Object());
  
          for(int i=0; i&lt;A.length; i++)
              if(all.containsKey(A[i]))
                  all.remove(A[i]);
  
          Iterator notOccur = all.keySet().iterator();
          if(notOccur.hasNext())
              return (int)notOccur.next();
  
          return 1;
  
      }
  }
  </code></pre>
  
  <p>The test result is:</p>
  
  <p><img src="https://i.stack.imgur.com/zIbzR.png" alt="enter image description here"></p>
  
  <p><strong>Can anyone explain me why I got this two wrong answers?</strong> Especially the first one, if there is only one element in the array, shouldn't the only right answer be 1? </p>