# Find The Count Of All Unique Numbers In A Given Range

The aim is to find the total count of numbers with unique digits in a given range using C# Language.

Approach

We divide this problem into 3 parts,
1. Traversing the range
2. Checking the Number
3. Returning the Count
Traversing the Range

We traverse the given range using any loop, for, while or do..while. Here I used for loop.

This is done to check all the possible numbers within a given range.

Checking the Number

For each number we check whether the digits are unique or not. For my solution I used Generic List from System.Collections.Generic to create a List that stores the digits a given number. I sorted the List to using Sort() method. The function CheckDigits() check each number by traversing this sorted list and returning a boolean value false if any of the consecutive digits of the List are same. If not this means all the digits in the List are Unique.

Returning the Count

Now the Unique function checks for every number in the given rabge, whether its unique or not. If the CheckDigits() function returns true, which means that the digits are unique, the count is incremented. At the end the count is returned to the main function and the output is displayed on the main screen.
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4.
5. public class Program{
6.     public static void Main(){
7.         //Getting the Upper Limit and Lower Limit Range
8.         int ll = Int32.Parse(Console.ReadLine());
9.         int ul = Int32.Parse(Console.ReadLine());
10.
11.         int output = Unique(ll,ul);
12.         Console.WriteLine(output);
13.     }
14.
15.    public static int Unique(int ll,int ul){
16.        int c = 0;
17.        for(int i=ll;i<=ul;i++){
18.            bool f = CheckDigits(i);
19.            if(f==true){
20.                c=c+1;
21.            }
22.        }
23.          //Returning Count
24.        return c;
25.    }
26.
27.    static bool CheckDigits(int i){
28.        List<int> d = new List<int>();
29.        while(i>0){
30.            int temp = i%10;
32.            i = i/10;
33.        }
34.
35.        //Sorting to make sure repetitive digits are consecutive.
36.        d.Sort();
37.
38.        //Make sure to run the loop for 1 less than the size of List else it would throw System.Out.Of.Bounds Error
39.        for(int j=0;j<d.Count()-1;j++){
40.            if(d[j]==d[j+1]){
41.                return false;
42.            }
43.        }
44.
45.        return true;
46.    }

There are many methods to solve this problem. This is one of the approaches using List. Do share your unique way of solving this problem!