Given a range, print all numbers having unique digits.
Examples :
Input : l = 10, r = 20
Output : 10 12 13 14 15 16 17 18 19 20 (Except 11)Input : l = 1, r = 10
Output : 1 2 3 4 5 6 7 8 9 10
Try It Yourself
Table of Content
[Naive Approach] Using String + Set – O((r − l + 1) × d) Time and O(d) Space
The idea is to convert every number into a string and store its digits inside a set. Since a set stores only unique elements, comparing the size of the string with the size of the set helps determine whether all digits are distinct. If both sizes are equal, the number contains unique digits.
- Traverse all numbers from
ltor - Convert each number into a string
- Insert all digits into a set
- Compare string length with set size
- If both are equal, add the number to the result list
#include <bits/stdc++.h>
using namespace std;
// Function to print unique
// numbers
vector<int> uniqueNumbers(int l, int r){
vector<int> ans;
// Iterate from l to r
for (int i = l; i <= r; i++) {
// Convert the no. to
// string
string s = to_string(i);
// Convert string to set using stl
set<int> uniDigits(s.begin(), s.end());
// Output if condition satisfies
if (s.size() == uniDigits.size()) {
ans.push_back(i);
}
}
return ans;
}
// Driver Code
int main()
{
// Input of the lower and
// higher limits
int l = 1, r = 20;
// Function Call
vector<int> result = uniqueNumbers(l, r);
for (int x : result)
cout << x << " ";
return 0;
}
import java.util.*;
class GFG {
// Function to print unique
// numbers
static ArrayList<Integer> uniqueNumbers(int l, int r){
ArrayList<Integer> ans = new ArrayList<>();
// Iterate from l to r
for (int i = l; i <= r; i++) {
// Convert the no. to
// string
String s = Integer.toString(i);
// Convert string to set using stl
HashSet<Character> uniDigits = new HashSet<>();
for (char ch : s.toCharArray()) {
uniDigits.add(ch);
}
// Output if condition satisfies
if (s.length() == uniDigits.size()) {
ans.add(i);
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Input of the lower and
// higher limits
int l = 1, r = 20;
// Function Call
ArrayList<Integer> result = uniqueNumbers(l, r);
for (int x : result)
System.out.print(x + " ");
}
}
# Function to print unique
# numbers
def uniqueNumbers(l, r):
ans = []
# Iterate from l to r
for i in range(l, r + 1):
# Convert the no. to
# string
s = str(i)
# Convert string to set using stl
uniDigits = set(s)
# Output if condition satisfies
if len(s) == len(uniDigits):
ans.append(i)
return ans
# Driver Code
# Input of the lower and
# higher limits
l = 1
r = 20
# Function Call
result = uniqueNumbers(l, r)
for x in result:
print(x, end=" ")
using System;
using System.Collections.Generic;
class GFG {
// Function to print unique
// numbers
static List<int> uniqueNumbers(int l, int r){
List<int> ans = new List<int>();
// Iterate from l to r
for (int i = l; i <= r; i++) {
// Convert the no. to
// string
string s = i.ToString();
// Convert string to set using stl
HashSet<char> uniDigits = new HashSet<char>(s);
// Output if condition satisfies
if (s.Length == uniDigits.Count) {
ans.Add(i);
}
}
return ans;
}
// Driver Code
static void Main()
{
// Input of the lower and
// higher limits
int l = 1, r = 20;
// Function Call
List<int> result = uniqueNumbers(l, r);
foreach (int x in result)
Console.Write(x + " ");
}
}
// Function to print unique
// numbers
function uniqueNumbers(l, r){
let ans = [];
// Iterate from l to r
for (let i = l; i <= r; i++) {
// Convert the no. to
// string
let s = i.toString();
// Convert string to set using stl
let uniDigits = new Set(s);
// Output if condition satisfies
if (s.length == uniDigits.size) {
ans.push(i);
}
}
return ans;
}
// Driver Code
// Input of the lower and
// higher limits
let l = 1, r = 20;
// Function Call
let result = uniqueNumbers(l, r);
for (let x of result)
process.stdout.write(x + " ");
Output
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20
[Efficient Approach] Using Digit Hashing – O((r − l + 1) × d) Time and O(1) Space
The idea is to use a boolean array of size
10to mark visited digits. While extracting digits, if a digit is encountered more than once, the number is not unique. Otherwise, it is added to the result list.
- Traverse all numbers from
ltor - For each number, extract digits one by one
- Use a boolean array to track already seen digits
- If any digit repeats, discard the number
- Otherwise, store it in the answer list
#include<bits/stdc++.h>
using namespace std;
// Function to print unique digit numbers
// in range from l to r.
vector<int> uniqueNumbers(int l, int r)
{
vector<int> ans;
// 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 d git 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)
ans.push_back(i);
}
return ans;
}
// Driver code
int main()
{
int l = 1, r = 20;
vector<int> result = uniqueNumbers(l, r);
for (int x : result)
cout << x << " ";
return 0;
}
import java.util.*;
class GFG {
// Function to print unique digit numbers
// in range from l to r.
static ArrayList<Integer> uniqueNumbers(int l, int r)
{
ArrayList<Integer> ans = new ArrayList<>();
// 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)
ans.add(i);
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int l = 1, r = 20;
ArrayList<Integer> result = uniqueNumbers(l, r);
for (int x : result)
System.out.print(x + " ");
}
}
# Function to print unique digit numbers
# in range from l to r.
def uniqueNumbers(l, r):
ans = []
# Start traversing the numbers
for i in range(l, r + 1):
num = i
visited = [False] * 10
# 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:
ans.append(i)
return ans
# Driver code
l = 1
r = 20
result = uniqueNumbers(l, r)
for x in result:
print(x, end=" ")
using System;
using System.Collections.Generic;
class GFG {
// Function to print unique digit numbers
// in range from l to r.
static List<int> uniqueNumbers(int l, int r)
{
List<int> ans = new List<int>();
// 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)
ans.Add(i);
}
return ans;
}
// Driver code
static void Main()
{
int l = 1, r = 20;
List<int> result = uniqueNumbers(l, r);
foreach (int x in result)
Console.Write(x + " ");
}
}
// Function to print unique digit numbers
// in range from l to r.
function uniqueNumbers(l, r)
{
let ans = [];
// Start traversing the numbers
for (let i = l; i <= r; i++)
{
let num = i;
let visited = new Array(10).fill(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 = Math.floor(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)
ans.push(i);
}
return ans;
}
// Driver code
let l = 1, r = 20;
let result = uniqueNumbers(l, r);
for (let x of result)
process.stdout.write(x + " ");
Output
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20