# Program to check if a given number is Lucky (all digits are different)

• Difficulty Level : Basic
• Last Updated : 05 Aug, 2022

A number is lucky if all digits of the number are different. How to check if a given number is lucky or not.
Examples:

```Input: n = 983
Output: true
All digits are different

Input: n = 9838
Output: false
8 appears twice```

We strongly recommend you to minimize your browser and try this yourself first.
The idea is to traverse through every digit of given number and mark the traversed digit as visited. Since the total number of digits is 10, we need a boolean array of size only 10 to mark visited digits.
Below is the implementation of above idea.

## C++

 `// C++ program to check if a given number is lucky``#include``using` `namespace` `std;` `// This function returns true if n is lucky``bool` `isLucky(``int` `n)``{``    ``// Create an array of size 10 and initialize all``    ``// elements as false. This array is used to check``    ``// if a digit is already seen or not.``    ``bool` `arr[10];``    ``for` `(``int` `i=0; i<10; i++)``        ``arr[i] = ``false``;` `    ``// Traverse through all digits of given number``    ``while` `(n > 0)``    ``{``        ``// Find the last digit``        ``int` `digit = n%10;` `        ``// If digit is already seen, return false``        ``if` `(arr[digit])``           ``return` `false``;` `        ``// Mark this digit as seen``        ``arr[digit] = ``true``;` `        ``// REmove the last digit from number``        ``n = n/10;``    ``}``    ``return` `true``;``}` `// Driver program to test above function.``int` `main()``{``    ``int` `arr[] = {1291, 897, 4566, 1232, 80, 700};``    ``int` `n = ``sizeof``(arr)/``sizeof``(arr[0]);` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to check if``// a given number is lucky` `class` `GFG``{``    ``// This function returns true if n is lucky``    ``static` `boolean` `isLucky(``int` `n)``    ``{``        ``// Create an array of size 10 and initialize all``        ``// elements as false. This array is used to check``        ``// if a digit is already seen or not.``        ``boolean` `arr[]=``new` `boolean``[``10``];``        ``for` `(``int` `i = ``0``; i < ``10``; i++)``            ``arr[i] = ``false``;``    ` `        ``// Traverse through all digits``        ``// of given number``        ``while` `(n > ``0``)``        ``{``            ``// Find the last digit``            ``int` `digit = n % ``10``;``    ` `            ``// If digit is already seen,``            ``// return false``            ``if` `(arr[digit])``            ``return` `false``;``    ` `            ``// Mark this digit as seen``            ``arr[digit] = ``true``;``    ` `            ``// Remove the last digit from number``            ``n = n / ``10``;``        ``}``        ``return` `true``;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``    ``int` `arr[] = {``1291``, ``897``, ``4566``, ``1232``, ``80``, ``700``};``        ``int` `n = arr.length;``    ` `        ``for` `(``int` `i = ``0``; i < n; i++)``            ``if``(isLucky(arr[i]))``                ``System.out.print(arr[i] + ``" is Lucky \n"``);``            ``else``            ``System.out.print(arr[i] + ``" is not Lucky \n"``);``    ``}``}` `// This code is contributed by Anant Agarwal.`

## Python3

 `# python program to check if a``# given number is lucky` `import` `math` `# This function returns true``# if n is lucky``def` `isLucky(n):``    ` `    ``# Create an array of size 10``    ``# and initialize all elements``    ``# as false. This array is``    ``# used to check if a digit``    ``# is already seen or not.``    ``ar ``=` `[``0``] ``*` `10``    ` `    ``# Traverse through all digits``    ``# of given number``    ``while` `(n > ``0``):``        ` `        ``#Find the last digit``        ``digit ``=` `math.floor(n ``%` `10``)` `        ``# If digit is already seen,``        ``# return false``        ``if` `(ar[digit]):``            ``return` `0` `        ``# Mark this digit as seen``        ``ar[digit] ``=` `1` `        ``# REmove the last digit``        ``# from number``        ``n ``=` `n ``/` `10``    ` `    ``return` `1` `# Driver program to test above function.``arr ``=` `[``1291``, ``897``, ``4566``, ``1232``, ``80``, ``700``]``n ``=` `len``(arr)` `for` `i ``in` `range``(``0``, n):``    ``k ``=` `arr[i]``    ``if``(isLucky(k)):``        ``print``(k, ``" is Lucky "``)``    ``else``:``        ``print``(k, ``" is not Lucky "``)``    ` `# This code is contributed by Sam007.`

## C#

 `// C# program to check if``// a given number is lucky``using` `System;` `class` `GFG {``    ` `    ``// This function returns true if``    ``// n is lucky``    ``static` `bool` `isLucky(``int` `n)``    ``{``        ` `        ``// Create an array of size 10``        ``// and initialize all elements``        ``// as false. This array is used``        ``// to check if a digit is``        ``// already seen or not.``        ``bool` `[]arr = ``new` `bool``[10];``        ` `        ``for` `(``int` `i = 0; i < 10; i++)``            ``arr[i] = ``false``;``    ` `        ``// Traverse through all digits``        ``// of given number``        ``while` `(n > 0)``        ``{``            ``// Find the last digit``            ``int` `digit = n % 10;``    ` `            ``// If digit is already seen,``            ``// return false``            ``if` `(arr[digit])``                ``return` `false``;``    ` `            ``// Mark this digit as seen``            ``arr[digit] = ``true``;``    ` `            ``// Remove the last digit``            ``// from number``            ``n = n / 10;``        ``}``        ` `        ``return` `true``;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main ()``    ``{``    ``int` `[]arr = {1291, 897, 4566, 1232,``                               ``80, 700};``        ``int` `n = arr.Length;``    ` `        ``for` `(``int` `i = 0; i < n; i++)``            ``if``(isLucky(arr[i]))``                ``Console.Write(arr[i] +``                        ``" is Lucky \n"``);``            ``else``            ``Console.Write(arr[i] +``                    ``" is not Lucky \n"``);``    ``}``}` `// This code is contributed by sam007.`

## PHP

 ` 0)``    ``{``        ``// Find the last digit``        ``\$digit` `= ``\$n` `% 10;` `        ``// If digit is already seen,``        ``// return false``        ``if` `(``\$arr``[``\$digit``])``        ``return` `false;` `        ``// Mark this digit as seen``        ``\$arr``[``\$digit``] = true;` `        ``// Remove the last digit``        ``// from number``        ``\$n` `= (int)(``\$n` `/ 10);``    ``}``    ``return` `true;``}` `// Driver Code``\$arr` `= ``array``(1291, 897, 4566,``             ``1232, 80, 700);``\$n` `= sizeof(``\$arr``);` `for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++)``    ``if``(isLucky(``\$arr``[``\$i``]))``        ``echo` `\$arr``[``\$i``] , ``" is Lucky \n"``;``    ``else``        ``echo` `\$arr``[``\$i``] , ``" is not Lucky \n"``;` `// This code is contributed by jit_t``?>`

## Javascript

 ``

Output:

```1291 is not Lucky
897 is Lucky
4566 is not Lucky
1232 is not Lucky
80 is Lucky
700 is not Lucky```

Time Complexity: O(d) where d is a number of digits in the input number.
Auxiliary Space: O(1)

Method 2:(using STL and sorting)

In this method we will firstly convert the number into a string. Then we will sort the whole string. Then we will compare every index element with the next index element. If both are equal then we will stop that position and will print that the given number is not lucky.

And if we do not get any index as discussed above then we will print that given number is lucky.

This whole task will be completed in O(K) time where K is the total number of digits of N.

Below is the implementation of the above approach:

## C++

 `// C++ code to check is the number``// lucky or not.``#include ``using` `namespace` `std;``void` `checklucky(string s,``int` `n)``{``    ``bool` `x = ``false``;` `    ``// traversing the whole string``    ``for` `(``int` `i = 0; i < s.length() - 1; i++)``    ``{``        ``// checking next element whether``        ``// it is equal or not``        ``if` `(s[i] == s[i + 1])``        ``{``            ``cout << n << ``" is not lucky number"``<

## Java

 `// Java code to check is the number``// lucky or not.``import` `java.util.*;` `class` `GFG {``    ``static` `void` `checklucky(``char``[] s, ``int` `n)``    ``{``        ``boolean` `x = ``false``;` `        ``// traversing the whole string``        ``for` `(``int` `i = ``0``; i < s.length - ``1``; i++)``        ``{``          ` `            ``// checking next element whether``            ``// it is equal or not``            ``if` `(s[i] == s[i + ``1``]) {``                ``System.out.println(``                    ``n + ``" is not lucky number"``);``                ``x = ``true``;``                ``break``;``            ``}``        ``}``        ``if` `(!x) {``            ``System.out.println(n + ``" is lucky number"``);``        ``}``    ``}` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n1 = ``1234``, n2 = ``5868``;``      ` `        ``// converting the number from``        ``// integer to string using``        ``// library function.``        ``char``[] s1 = String.valueOf(n1).toCharArray();``        ``char``[] s2 = String.valueOf(n2).toCharArray();` `        ``// sorting the string``        ``Arrays.sort(s1);``        ``Arrays.sort(s2);` `        ``// function calling``        ``checklucky(s1, n1);``        ``checklucky(s2, n2);``    ``}``}` `// this code is contribut by phasing17`

## Python3

 `# Python3 code to check is the number``# lucky or not.``def` `checklucky(s, n):``    ``x ``=` `False` `    ``# traversing the whole string``    ``for` `i ``in` `range``(``len``(s) ``-` `1``):` `        ``# checking next element whether``        ``# it is equal or not``        ``if` `(s[i] ``=``=` `s[i ``+` `1``]):``            ``print``(n, ``"is not a lucky number"``)``            ``return` `    ``if` `not` `x:``        ``print``(n, ``"is a lucky number"``)`  `# Driver Code``n1 ``=` `1234``n2 ``=` `5868` `# converting the number from``# integer to string``s1 ``=` `str``(n1)``s2 ``=` `str``(n2)` `# sorting the string``s1 ``=` `"".join(``sorted``(s1))``s2 ``=` `"".join(``sorted``(s2))` `# function calling``checklucky(s1, n1)``checklucky(s2, n2)`  `# this code is contribut by phasing17`

## C#

 `// C# code to check is the number``// lucky or not.` `using` `System;``using` `System.Collections.Generic;` `class` `GFG {``    ``static` `void` `checklucky(``char``[] s, ``int` `n)``    ``{``        ``bool` `x = ``false``;` `        ``// traversing the whole string``        ``for` `(``int` `i = 0; i < s.Length - 1; i++) {``            ``// checking next element whether``            ``// it is equal or not``            ``if` `(s[i] == s[i + 1]) {``                ``Console.WriteLine(n``                                  ``+ ``" is not lucky number"``);``                ``x = ``true``;``                ``break``;``            ``}``        ``}``        ``if` `(!x) {``            ``Console.WriteLine(n + ``" is lucky number"``);``        ``}``    ``}` `    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int` `n1 = 1234, n2 = 5868;``        ``// converting the number from``        ``// integer to string using``        ``// library function.``        ``char``[] s1 = Convert.ToString(n1).ToCharArray();``        ``char``[] s2 = Convert.ToString(n2).ToCharArray();` `        ``// sorting the string``        ``Array.Sort(s1);``        ``Array.Sort(s2);` `        ``// function calling``        ``checklucky(s1, n1);``        ``checklucky(s2, n2);``    ``}``}` `// this code is contribut by phasing17`

## Javascript

 `// JavaScript code to check is the number``// lucky or not.` `function` `checklucky(s, n)``{``    ``let x = ``false``;` `    ``// traversing the whole string``    ``for` `(``var` `i = 0; i < s.length - 1; i++)``    ``{``        ``// checking next element whether``        ``// it is equal or not``        ``if` `(s[i] == s[i + 1])``        ``{``            ``console.log(n + ``" is not lucky number"``);``            ``x = ``true``;``            ``break``;``        ``}``    ``}``    ``if` `(!x)``    ``{``        ``console.log(n + ``" is lucky number"``);``    ``}``}` `let n1 = 1234,n2=5868;` `// converting the number from``// integer to string   ``let s1 = n1.toString();``let s2 = n2.toString();` `// sorting the string``s1 = s1.split(``""``);``s2 = s2.split(``""``);``s1.sort();``s2.sort();``s1 = s1.join(``""``);``s2 = s2.join(``""``);` `// function calling``checklucky(s1,n1);``checklucky(s2,n2);` `// this code is contribut by phasing17`

Output

```1234 is lucky number
5868 is not lucky number```

Time Complexity: O(n1*logn1 + n2*logn2), where n1 and n2 represents the lengths of the given strings.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

My Personal Notes arrow_drop_up