# Print a number strictly less than a given number such that all its digits are distinct.

Given a positive number n, print a number less than n such that all its digits are distinct.

Examples:

```Input : 1134
Output : 1098
1098  is the largest number smaller than
1134 such that all digits are distinct.

Input : 4559
Output : 4539
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The problem can easily be solved by using counting. Firstly, loop through numbers less than n and for each number count the frequency of the digits using count array. If all the digits occur only once than we print that number. The answer always exists so there is no problem of infinite loop.

## C++

 `// CPP program to find a number less than ` `// n such that all its digits are distinct ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find a number less than ` `// n such that all its digits are distinct ` `int` `findNumber(``int` `n) ` `{ ` `    ``// looping through numbers less than n ` `    ``for` `(``int` `i = n - 1; >=0 ; i--) { ` ` `  `        ``// initializing a hash array ` `        ``int` `count = { 0 };  ` ` `  `        ``int` `x = i; ``// creating a copy of i ` ` `  `        ``// initializing variables to compare lengths of digits ` `        ``int` `count1 = 0, count2 = 0; ` ` `  `        ``// counting frequency of the digits ` `        ``while` `(x) { ` `            ``count[x % 10]++;  ` `            ``x /= 10; ` `            ``count1++; ` `        ``} ` ` `  `        ``// checking if each digit is present once ` `        ``for` `(``int` `j = 0; j < 10; j++) { ` `            ``if` `(count[j] == 1)  ` `                ``count2++; ` `        ``}          ` `        ``if` `(count1 == count2) ` `            ``return` `i; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 8490; ` `    ``cout << findNumber(n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find a number less than ` `// n such that all its digits are distinct ` ` `  `class` `GFG{ ` `// Function to find a number less than ` `// n such that all its digits are distinct ` `static` `int` `findNumber(``int` `n) ` `{ ` `    ``// looping through numbers less than n ` `    ``for` `(``int` `i = n - ``1``;i >=``0` `; i--) { ` ` `  `        ``// initializing a hash array ` `        ``int``[] count=``new` `int``[``10``];  ` ` `  `        ``int` `x = i; ``// creating a copy of i ` ` `  `        ``// initializing variables to compare lengths of digits ` `        ``int` `count1 = ``0``, count2 = ``0``; ` ` `  `        ``// counting frequency of the digits ` `        ``while` `(x>``0``) { ` `            ``count[x % ``10``]++;  ` `            ``x /= ``10``; ` `            ``count1++; ` `        ``} ` ` `  `        ``// checking if each digit is present once ` `        ``for` `(``int` `j = ``0``; j < ``10``; j++) { ` `            ``if` `(count[j] == ``1``)  ` `                ``count2++; ` `        ``}          ` `        ``if` `(count1 == count2) ` `            ``return` `i; ` `    ``} ` `    ``return` `-``1``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``8490``; ` `    ``System.out.println(findNumber(n)); ` `} ` `} ` `// This code is contributed by mits `

## Python3

 `# python 3 program to find a number less than ` `# n such that all its digits are distinct ` ` `  `# Function to find a number less than ` `# n such that all its digits are distinct ` `def` `findNumber(n): ` `    ``# looping through numbers less than n ` `    ``i ``=` `n``-``1` `    ``while``(i>``=``0``): ` `        ``# initializing a hash array ` `        ``count ``=` `[``0` `for` `i ``in` `range``(``10``)]  ` ` `  `        ``x ``=` `i ` `        ``# creating a copy of i ` ` `  `        ``# initializing variables to compare lengths of digits ` `        ``count1 ``=` `0` `        ``count2 ``=` `0` ` `  `        ``# counting frequency of the digits ` `        ``while` `(x): ` `         `  `            ``count[x``%``10``] ``+``=` `1` `            ``x ``=` `int``(x ``/` `10``) ` `            ``count1 ``+``=` `1` `         `  `        ``# checking if each digit is present once ` `        ``for` `j ``in` `range``(``0``,``10``,``1``): ` `            ``if` `(count[j] ``=``=` `1``): ` `                ``count2 ``+``=` `1` `                 `  `        ``if` `(count1 ``=``=` `count2): ` `            ``return` `i ` `        ``i ``-``=` `1` `     `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``n ``=` `8490` `    ``print``(findNumber(n)) ` ` `  `# This code is implemented by ` `# Surendra_Gangwar  `

## C#

 `// C# program to find a number less than ` `// n such that all its digits are distinct ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to find a number less than ` `// n such that all its digits are distinct ` `static` `int` `findNumber(``int` `n) ` `{ ` `    ``// looping through numbers less than n ` `    ``for` `(``int` `i = n - 1; i >= 0; i--) ` `    ``{ ` ` `  `        ``// initializing a hash array ` `        ``int``[] count = ``new` `int``;  ` ` `  `        ``int` `x = i; ``// creating a copy of i ` ` `  `        ``// initializing variables to compare ` `        ``// lengths of digits ` `        ``int` `count1 = 0, count2 = 0; ` ` `  `        ``// counting frequency of the digits ` `        ``while` `(x > 0) ` `        ``{ ` `            ``count[x % 10]++;  ` `            ``x /= 10; ` `            ``count1++; ` `        ``} ` ` `  `        ``// checking if each digit is ` `        ``// present once ` `        ``for` `(``int` `j = 0; j < 10; j++)  ` `        ``{ ` `            ``if` `(count[j] == 1)  ` `                ``count2++; ` `        ``}      ` `        ``if` `(count1 == count2) ` `            ``return` `i; ` `    ``} ` `    ``return` `-1; ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `n = 8490; ` `    ``Console.WriteLine(findNumber(n)); ` `} ` `} ` ` `  `// This code is contributed by akt_mit `

## PHP

 `= 0 ; ``\$i``--)  ` `    ``{ ` ` `  `        ``// initializing a hash array ` `        ``\$count` `= ``array_fill``(0, 10, 0);  ` ` `  `        ``\$x` `= ``\$i``; ``// creating a copy of i ` ` `  `        ``// initializing variables to  ` `        ``// compare lengths of digits ` `        ``\$count1` `= 0; ``\$count2` `= 0; ` ` `  `        ``// counting frequency of the digits ` `        ``while` `(``\$x``)  ` `        ``{ ` `            ``\$count``[``\$x` `% 10]++;  ` `            ``\$x` `= (int)(``\$x` `/ 10); ` `            ``\$count1``++; ` `        ``} ` ` `  `        ``// checking if each digit  ` `        ``// is present once ` `        ``for` `(``\$j` `= 0; ``\$j` `< 10; ``\$j``++)  ` `        ``{ ` `            ``if` `(``\$count``[``\$j``] == 1)  ` `                ``\$count2``++; ` `        ``}      ` `        ``if` `(``\$count1` `== ``\$count2``) ` `            ``return` `\$i``; ` `    ``} ` `} ` ` `  `// Driver code ` `\$n` `= 8490; ` `echo` `findNumber(``\$n``); ` ` `  `// This code is contributed  ` `// by Akanksha Rai ` `?> `

Output:

```8479
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.