title: To count unique digits in a given number N in java
body: <p>Given a number N, for example, take 1091, here the count of digits is 4 but the count of unique digits is 3 i.e. 1, 0 &amp; 9 are unique (since 1 is repeated).</p>
  <p>I have tried breaking the number into individual digits and adding it to ArrayList and later converting it to an array. Then, iterating through the array and increasing the count of unique digits by 1 every time I got a unique digit in the array, But I have not got the required output. Kindly someone help in finding the unique digits count in a given number in Java.</p>
  <pre class="lang-java prettyprint-override"><code>import java.util.ArrayList;
  
  public class UniqueDigits {
      static int uniqueDigitCount(int n) {
          ArrayList&lt;Integer&gt; ar = new ArrayList&lt;&gt;();
          int temp = n;
          int count = 1;
          do {
              ar.add(temp % 10);
              temp /= 10;
          }
          while (temp &gt; 0);
          Integer[] arr = new Integer[ar.size()];
          arr = ar.toArray(arr);
          if (arr.length &gt; 0) {
              for (int i = 0; i &lt; arr.length - 1; i++) {
                  if (arr[i] != arr[i + 1]) {
  
                      count++;
                  }
              }
              return count;
          } else {
              return 0;
          }
      }
  
      public static void main(String[] args) {
          System.out.println(uniqueDigitCount(1091));
      }
  }
  </code></pre>