title: Java - Find maximum number of duplicates within an array
body: <p>I am utilizing a <code>HashSet</code> for finding the maximum number of duplicates of a value in a <strong>sorted</strong> <code>Integer</code> array. But my algorithm doesn't seem to work, not returning the desired results.</p>
  
  <pre><code>Set variables storing the number of duplicates found (0), and the maximum number of duplicates (0). 
  Set a HashSet that stores the unique values of an array.
  Sort the array to be ready for comparison.
  
  Loop through each value of the array
      If the HashSet of unique values contains the current value:
          Increment the duplicate Count
  
      If the currentValue is not equal to the previous value:
          If the duplicateCount is greater than the maximum Count:
              maximumCount becomes duplicateCount
              Reset duplicateCount to 0
  </code></pre>
  
  <p><strong>Java Code</strong>:</p>
  
  <pre><code>HashSet&lt;Integer&gt; uniqueValues = new HashSet&lt;Integer&gt;(valueSequenceList);
  
  int duplicateCount = 0;
  int maxCount = 0;
  Arrays.sort(valueSequence);
  
  for (int i = 0; i &lt; valueSequence.length; i++)
  {
      if (uniqueValues.contains(valueSequence[i]))
      {
          duplicateCount++;
      }
      if (i &gt; 0 &amp;&amp; valueSequence[i] != valueSequence[i-1])
      {
          if (duplicateCount &gt; maxCount)
          {
              maxCount = duplicateCount;
              duplicateCount = 0;
          }
      }
  }
  </code></pre>
  
  <p><strong>Example</strong>:
  <br><em>Input</em>: [4, 4, 10, 4, 10]
  <br><em>Output</em>: 4 Duplicates (There are supposed to be a maximum of 3 duplicates - Total number of values that are the same).</p>