Numbers having Unique (or Distinct) digits
Last Updated :
23 Dec, 2023
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
Approach:
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.
Implementation:
C++14
#include <array>
#include <iostream>
void printUnique( int l, int r)
{
for ( int i = l; i <= r; ++i) {
int num = i;
std::array< bool , 10> visited = { false };
while (num != 0) {
if (visited[num % 10])
break ;
visited[num % 10] = true ;
num = num / 10;
}
if (num == 0)
std::cout << i << " " ;
}
}
int main()
{
int l = 1, r = 20;
printUnique(l, r);
return 0;
}
|
C++
#include<bits/stdc++.h>
using namespace std;
void printUnique( int l, int r)
{
for ( int i=l ; i<=r ; i++)
{
int num = i;
bool visited[10] = { false };
while (num)
{
if (visited[num % 10])
break ;
visited[num%10] = true ;
num = num/10;
}
if (num == 0)
cout << i << " " ;
}
}
int main()
{
int l = 1, r = 20;
printUnique(l, r);
return 0;
}
|
Java
public class UniqueDigitNumbers {
static void printUnique( int l, int r) {
for ( int i = l; i <= r; ++i) {
int num = i;
boolean [] visited = new boolean [ 10 ];
while (num != 0 ) {
if (visited[num % 10 ]) {
break ;
}
visited[num % 10 ] = true ;
num = num / 10 ;
}
if (num == 0 ) {
System.out.print(i + " " );
}
}
}
public static void main(String[] args) {
int l = 1 , r = 20 ;
printUnique(l, r);
}
}
|
Python3
def printUnique(l,r):
for i in range (l, r + 1 ):
num = i;
visited = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ];
while (num):
if visited[num % 10 ] = = 1 :
break ;
visited[num % 10 ] = 1 ;
num = ( int )(num / 10 );
if num = = 0 :
print (i, end = " " );
l = 1 ;
r = 20 ;
printUnique(l, r);
|
C#
using System;
public class GFG {
static void printUnique( int l, int r)
{
for ( int i = l ; i <= r ; i++)
{
int num = i;
bool []visited = new bool [10];
while (num != 0)
{
if (visited[num % 10])
break ;
visited[num % 10] = true ;
num = num / 10;
}
if (num == 0)
Console.Write(i + " " );
}
}
public static void Main()
{
int l = 1, r = 20;
printUnique(l, r);
}
}
|
Javascript
<script>
function printUnique(l, r)
{
for (let i=l ; i<=r ; i++)
{
let num = i;
let visited = new Array(10);
while (num)
{
if (visited[num % 10])
break ;
visited[num%10] = true ;
num = Math.floor(num/10);
}
if (num == 0)
document.write(i + " " );
}
}
let l = 1, r = 20;
printUnique(l, r);
</script>
|
PHP
<?php
function printUnique( $l , $r )
{
for ( $i = $l ; $i <= $r ; $i ++)
{
$num = $i ;
$visited = (false);
while ( $num )
{
if ( $visited [ $num % 10])
$visited [ $num % 10] = true;
$num = (int) $num / 10;
}
if ( $num == 0)
echo $i , " " ;
}
}
$l = 1; $r = 20;
printUnique( $l , $r );
?>
|
Output
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20
Time Complexity : O(nlogn)
Auxiliary Space: O(1)
Another Approach: Use set STL for C++ and Java Collections for Java in order to check if a number has only unique digits. Then we can compare the size of string s formed from a given number and newly created set. For example, let us consider the number 1987, then we can convert the number into the string,
Implementation:
C++
int n;
cin>>n;
string s = to_string(n);
|
Java
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
string s = String.valueof(n);
|
Python3
n = int ( input ())
s = str (n)
|
C#
int n = Convert.ToInt32(Console.ReadLine());
string s = Convert.ToString(n);
|
Javascript
After that, initialize a set with the contents of string s.
C++
set< int > uniDigits(s.begin(), s.end());
|
Java
HashSet<Integer> uniDigits = new HashSet<Integer>();
for ( int i:s.tocharArray())
{
uniDigits.add(i);
}
|
Python3
C#
var uniDigits = new HashSet< char >(s.ToCharArray());
|
Javascript
let uniDigits = new Set();
|
Then we can compare the size of string s and newly created set uniDigits.
Here is the code for the above approach:
C++14
#include <iostream>
#include <unordered_set>
void printUnique( int l, int r) {
for ( int i = l; i <= r; ++i) {
std::string s = std::to_string(i);
std::unordered_set< char > uniDigits(s.begin(), s.end());
if (s.length() == uniDigits.size()) {
std::cout << i << " " ;
}
}
}
int main() {
int l = 1, r = 20;
printUnique(l, r);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
void printUnique( int l, int r){
for ( int i = l; i <= r; i++) {
string s = to_string(i);
set< int > uniDigits(s.begin(), s.end());
if (s.size() == uniDigits.size()) {
cout << i << " " ;
}
}
}
int main()
{
int l = 1, r = 20;
printUnique(l, r);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void printUnique( int l, int r)
{
for ( int i = l; i <= r; i++)
{
String s = String.valueOf(i);
HashSet<Integer> uniDigits = new HashSet<Integer>();
for ( int c : s.toCharArray())
uniDigits.add(c);
if (s.length() == uniDigits.size())
{
System.out.print(i+ " " );
}
}
}
public static void main(String[] args)
{
int l = 1 , r = 20 ;
printUnique(l, r);
}
}
|
Python3
def print_unique(l, r):
for i in range (l, r + 1 ):
s = str (i)
uni_digits = set (s)
if len (s) = = len (uni_digits):
print (i, end = " " )
l = 1
r = 20
print_unique(l, r)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void printUnique( int l, int r)
{
for ( int i = l; i <= r; i++)
{
String s = String.Join( "" , i);
HashSet< int > uniDigits = new HashSet< int >();
foreach ( int c in s.ToCharArray())
uniDigits.Add(c);
if (s.Length == uniDigits.Count)
{
Console.Write(i + " " );
}
}
}
public static void Main(String[] args)
{
int l = 1, r = 20;
printUnique(l, r);
}
}
|
Javascript
<script>
function printUnique(l, r) {
for (let i = l; i <= r; i++) {
let s = String(i);
let uniDigits = new Set();
for (let c of s.split( "" ))
uniDigits.add(c);
if (s.length == uniDigits.size) {
document.write(i + " " );
}
}
}
let l = 1, r = 20;
printUnique(l, r);
</script>
|
Output
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...