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

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 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<iostream>
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<n; i++)
        isLucky(arr[i])? cout << arr[i] << " is Lucky \n":
                         cout << arr[i] << " is not Lucky \n";
    return 0;
}

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.

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 number of digits in 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



My Personal Notes arrow_drop_up

Improved By : Sam007



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.