title: 3SUM: avoid duplicates
body: <p>Problem:
  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.</p>
  
  <p>To avoid duplicates I use an if-clause in the while loop, and I was wondering why this does not work. Thanks!</p>
  
  <pre><code>public List&lt;List&lt;Integer&gt;&gt; threeSum(int[] nums) {
      List&lt;List&lt;Integer&gt;&gt; result = new ArrayList&lt;List&lt;Integer&gt;&gt;();
      Arrays.sort(nums);
  
      for (int i = 0; i&lt;nums.length; i++){
          if (i &gt; 0 &amp;&amp; nums[i] == nums[i-1]){
              continue;
          }
          int target = -1*nums[i];
          int start = i+1;
          int end = nums.length-1;
  
          while (start &lt; end){
  
              if (nums[start] == nums[start+1]){
                  start++;
              } 
  
              if (nums[end] == nums[end--]){
                  end--;
              } 
  
              if (nums[start]+nums[end] == target){
                  System.out.printf("found");
                  List&lt;Integer&gt; temp = new ArrayList&lt;Integer&gt;();
                  temp.add(nums[i]);
                  temp.add(nums[start]);
                  temp.add(nums[end]);
                  result.add(temp);
                  start++;
                  end--;
              } else if (nums[start]+nums[end] &lt; target) {
                  start++;
              } else {
                  end--;
              }
          }
      }
      return result;
  </code></pre>
  
  <p>I was testing on [-1,0,1] and my code returned an empty list. I tried to debug and printed some messages, and it seems that when i=1, in the while loop, it first enters </p>
  
  <pre><code>if (nums[start] == nums[start+1]){
                  start++;
  } 
  </code></pre>
  
  <p>But nums[start] does not equal to nums[start+1] when start=1. This is quite bizzare. Can someone explain it to me?</p>