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

08 Jun, 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++
Java
Python3
C#
PHP
Javascript
// C++ program to check if a given number is lucky#include<iostream>using namespace std; // This function returns true if n is luckybool 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<n; i++)        isLucky(arr[i])? cout << arr[i] << " is Lucky \n":                         cout << arr[i] << " is not Lucky \n";    return 0;}
// 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.
# python program to check if a# given number is lucky import math # This function returns true# if n is luckydef 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# program to check if// a given number is luckyusing 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// PHP program to check if a given// number is lucky // This function returns true// if n is luckyfunction 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.    $arr = array();    for ($i = 0; $i < 10; $i++)        $arr[$i] = false;     // Traverse through all digits    // of given number    while ($n > 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?>
<script> // Javascript program to check if a given number is lucky // This function returns true if n is luckyfunction 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.    var arr=Array(10).fill(0);    for (var i=0; i<10; i++)        arr[i] = false;     // Traverse through all digits of given number    while (n > 0)    {        // Find the last digit        var 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 = parseInt(n/10);    }    return true;} // Driver program to test above function.var arr = [1291, 897, 4566, 1232, 80, 700]var n = arr.length;for (var i=0; i<n; i++)    isLucky(arr[i])? document.write( arr[i] + " is Lucky<br>"):                    document.write(arr[i] + " is not Lucky<br>"); </script>
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)This article is contributed by Himanshu. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
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 <bits/stdc++.h>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"<<endl;            x = true;            break;        }    }    if (!x)    {        cout << n << " is lucky number"<<endl;    }}int main(){    int n1 = 1234,n2=5868;    // converting the number from    // integer to string using    // C++ STL function.    string s1 = to_string(n1);    string s2 = to_string(n2);         // sorting the string    sort(s1.begin(), s1.end());    sort(s2.begin(), s2.end());    //function calling    checklucky(s1,n1);    checklucky(s2,n2);    return 0;} //this code is contribut by Machhaliya Muhammad
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.
Sam007
jit_t
rutvik_56
Shubham Pandey 1
shaheeneallamaiqbal
samim2000
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operators in C / C++
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Modulo 10^9+7 (1000000007)
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number
Program to find sum of elements in a given array
Merge two sorted arrays with O(1) extra space