title: Count different values in array in Java
body: <p>I'm writing a code where I have an int[a] and the method should return the number of unique values. Example: {1} = 0 different values, {3,3,3} = 0 different values, {1,2} = 2 different values, {1,2,3,4} = 4 different values etc. <strong>I am not allowed to sort the array</strong>. </p>
  
  <p>The thing is that my method doesn't work probably. There is something wrong with my for statement and I can't figure it out.</p>
  
  <pre><code>public class Program
  {
  
      public static void main(String[] args)
      {
          int[] a = {1, 2, 3, 1};
  
          System.out.println(differentValuesUnsorted(a));
          //run: 4 //should be 3
      }
  
  public static int differentValuesUnsorted(int[] a)
  {
      int values;      //values of different numbers
  
      if (a.length &lt; 2)
      {
          return values = 0;
      }else if (a[0] == a[1])
      {
          return values = 0;
      }else
      {
          values = 2;
      } 
  
      int numberValue = a[0];
      for (int i = a[1]; i &lt; a.length; i++)
      {
          if (a[i] != numberValue)
          {
               numberValue++;
               values++;
          }
      }
          return values;
      }
  }
  </code></pre>
  
  <p>Can anybody help?   </p>