Given a range, print all numbers having unique digits.
Examples :
Input : 10 20 Output : 10 12 13 14 15 16 17 18 19 20 (Except 11) Input : 1 10 Output : 1 2 3 4 5 6 7 8 9 10
As the problem is pretty simple, the only thing to be done is :- 1- Find the digits one by one and keep marking visited digits. 2- If all digits occurs one time only then print that number. 3- Else not.
C++
// C++ implementation to find unique digit // numbers in a range #include<bits/stdc++.h> using namespace std; // Function to print unique digit numbers // in range from l to r. void printUnique(int l, int r) { // Start traversing the numbers for (int i=l ; i<=r ; i++) { int num = i; bool visited[10] = {false}; // Find digits and maintain its hash while (num) { // if a digit occurs more than 1 time // then break if (visited[num % 10]) break; visited[num%10] = true; num = num/10; } // num will be 0 only when above loop // doesn't get break that means the // number is unique so print it. if (num == 0) cout << i << " "; } } // Driver code int main() { int l = 1, r = 20; printUnique(l, r); return 0; } |
chevron_right
filter_none
Java
// Java implementation to find unique digit // numbers in a range class Test { // Method to print unique digit numbers // in range from l to r. static void printUnique(int l, int r) { // Start traversing the numbers for (int i=l ; i<=r ; i++) { int num = i; boolean visited[] = new boolean[10]; // Find digits and maintain its hash while (num != 0) { // if a digit occurs more than 1 time // then break if (visited[num % 10]) break; visited[num%10] = true; num = num/10; } // num will be 0 only when above loop // doesn't get break that means the // number is unique so print it. if (num == 0) System.out.print(i + " "); } } // Driver method public static void main(String args[]) { int l = 1, r = 20; printUnique(l, r); } } |
chevron_right
filter_none
Python3
# Python3 implementation # to find unique digit # numbers in a range # Function to print # unique digit numbers # in range from l to r. def printUnique(l,r): # Start traversing # the numbers for i in range (l, r + 1): num = i; visited = [0,0,0,0,0,0,0,0,0,0]; # Find digits and # maintain its hash while (num): # if a digit occurs # more than 1 time # then break if visited[num % 10] == 1: break; visited[num % 10] = 1; num = (int)(num / 10); # num will be 0 only when # above loop doesn't get # break that means the # number is unique so # print it. if num == 0: print(i, end = " "); # Driver code l = 1; r = 20; printUnique(l, r); # This code is # contributed by mits |
chevron_right
filter_none
C#
// C# implementation to find unique digit // numbers in a range using System; public class GFG { // Method to print unique digit numbers // in range from l to r. static void printUnique(int l, int r) { // Start traversing the numbers for (int i = l ; i <= r ; i++) { int num = i; bool []visited = new bool[10]; // Find digits and maintain // its hash while (num != 0) { // if a digit occurs more // than 1 time then break if (visited[num % 10]) break; visited[num % 10] = true; num = num / 10; } // num will be 0 only when // above loop doesn't get // break that means the number // is unique so print it. if (num == 0) Console.Write(i + " "); } } // Driver method public static void Main() { int l = 1, r = 20; printUnique(l, r); } } // This code is contributed by Sam007. |
chevron_right
filter_none
PHP
<?php // PHP implementation to find unique // digit numbers in a range // Function to print unique digit // numbers in range from l to r. function printUnique($l, $r) { // Start traversing the numbers for ($i = $l ; $i <= $r; $i++) { $num = $i; $visited = (false); // Find digits and // maintain its hash while ($num) { // if a digit occurs more // than 1 time then break if ($visited[$num % 10]) $visited[$num % 10] = true; $num = (int)$num / 10; } // num will be 0 only when above // loop doesn't get break that // means the number is unique // so print it. if ($num == 0) echo $i , " "; } } // Driver code $l = 1; $r = 20; printUnique($l, $r); // This code is contributed by aj_36 ?> |
chevron_right
filter_none
Output :
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20
This article is contributed by Sahil Chhabra. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
Recommended Posts:
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers from given range having odd digits at odd places and even digits at even places
- Count distinct pairs from two arrays having same sum of digits
- Find Next number having distinct digits from the given number N
- Print all numbers less than N with at-most 2 unique digits
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Minimum digits to be removed to make either all digits or alternating digits same
- Number of distinct ways to represent a number as sum of K unique primes
- Count of N-digit numbers with all distinct digits
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Count of distinct numbers formed by shuffling the digits of a large number N
- Count subsets having distinct even numbers
- Length of longest subarray having only K distinct Prime Numbers
- Count of unique digits in a given number N
- Append digits to the end of dupicate strings to make all strings in an array unique
- Divide the array into minimum number of sub-arrays having unique elements
- Subsequence pair from given Array having all unique and all same elements respectively
- Largest Sum Contiguous Subarray having unique elements
- Numbers with sum of digits equal to the sum of digits of its all prime factor

