title: questions with a while loop that has two pointers
body: <p>I am trying to find duplicate segments in a Java array that was sorted by Arrays.sort(). I am expecting the same ints to form a duplicate segment in the array. For example, after sorting, the array is: {1, 2, 3, 3, 3, 3, 5, 6, 8, 8, 8, 8, 8, 9, 9, 9, 9}</p>
  
  <p>I want to implement my following idea to find the duplicate segments: I want to use a while-loop with two pointers (i, and j). </p>
  
  <p>1.) Let i start from the index 0, and let j start the last index (N-1). 
  2.) Keep i at the index 0 while doing j--. When j reaches the next index of i and no segments found, increment i by 1, and reinitialize j to index N-1
  3. ) repeat steps 1 and 2. If a segment is found, increment i to j's current index, and reinitialize j to the index N-1.
  4.) exit the while loop when i==j.</p>
  
  <p>Below is my attempt, but it does not from my execution.</p>
  
  <pre><code>int[] test = new int[] {1, 2, 3, 3, 3, 3, 5, 6, 8, 8, 8, 8, 8, 9, 9, 9, 9};
          int i = 0;
          int j = test.length - 1;
          int[] copySegment;
  
          while (j &gt; i)
          {
              if (test[j] == test[i])
              {
                  int segmentLength = j - i + 1;
                  copySegment = new int[segmentLength];
                  for (int k = j; k &gt;= i; k--)
                  {
                      copySegment[segmentLength--] = test[k];
                  }
                  for (int e : copySegment)
                  {
                      System.out.print(e + " ");
                  }
              }
              j--;
              i++;
          }
  </code></pre>