How to Check whether a number is unique | Python, Java , C & C++ Program to Check whether the number is unique | How to check if the input number is unique or not in python, Java, C, C++


How to Check whether a number is unique | Python, Java , C & C++ Program to Check whether the number is unique | How to check if the input number is unique or not in python, Java, C, C++


Explanation of the Code:  Check whether a number is unique in Python, Java , C & C++

The problem asks us to determine if a given number has unique digits, i.e., no digit repeats in the number.

Input and Output: (Check whether a number is unique in Python, Java , C & C++)


  • The program asks the user to input a number n.
  • The program checks whether all the digits of the number n are unique. If they are, it outputs that the number is unique; otherwise, it outputs that the number is not unique.

Algorithm (Main Steps): (Check whether a number is unique in Python, Java , C & C++)


  • The function isUnique(n) checks if the digits of the number n are unique.
  • For each digit from 0 to 9, the function counts how many times that digit appears in n.
  •  It does so by repeatedly extracting the last digit of n (using num % 10), comparing it with the current digit i, and reducing n by dividing it by 10.
  • If any digit appears more than once (i.e., count > 1), the function returns false, indicating the number is not unique.
  • If no digit is repeated, the function returns true.


Steps in Detail: (Check whether a number is unique in Python, Java , C & C++)

How to check if a number has unique digits in Python and Java Unique digit number checker in C and C++ Best way to determine if a number is unique in Python C++ program to check for unique digits in a number JavaScript vs Python for unique number check How to identify unique numbers in an array Efficient algorithm to check unique digits in a number Step-by-step guide to check unique numbers in C++ How to write a program to find unique numbers in Java Easy method to check number uniqueness in programming
How to Check whether a number is unique | Python, Java , C & C++ Program to Check whether the number is unique | How to check if the input number is unique or not in python, Java, C, C++

  • The outer loop runs through digits from 0 to 9.
  • The inner loop processes the number n by extracting each digit and counting its occurrences.
  • The isUnique function returns true if all digits in the number are unique (no repetition), and false otherwise.


Java Code


import java.util.Scanner;

public class uniqueNumber {
public static boolean isUnique(int n)
{
for(int i=0; i<9; i++) {
int c = 0, num = n;
while(num != 0) {
int d = num%10;
if(d == i)
c++;
num = num/10;
}
if(c > 1)
return false;
}
return true;
}
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
n=sc.nextInt();
if(isUnique(n))
{
System.out.println(n+" is a Unique Number.");
}
else
{
System.out.println(n+" is not a Unique Number.");
}
sc.close();
}
}




// |VICTORY| The code ran successfully !



Python:


def is_unique(n):
    for i in range(10):
        count = 0
        num = n
        while num != 0:
            d = num % 10
            if d == i:
                count += 1
            num = num // 10
        if count > 1:
            return False
    return True

n = int(input("Enter a number: "))
if is_unique(n):
    print(f"{n} is a Unique Number.")
else:
    print(f"{n} is not a Unique Number.")



C:



#include <stdio.h>

int isUnique(int n) {
    for (int i = 0; i < 10; i++) {
        int count = 0, num = n;
        while (num != 0) {
            int d = num % 10;
            if (d == i)
                count++;
            num = num / 10;
        }
        if (count > 1)
            return 0;  // not a unique number
    }
    return 1;  // unique number
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);

    if (isUnique(n)) {
        printf("%d is a Unique Number.\n", n);
    } else {
        printf("%d is not a Unique Number.\n", n);
    }

    return 0;
}


C++:

#include <iostream>
using namespace std;

bool isUnique(int n) {
    for (int i = 0; i < 10; i++) {
        int count = 0, num = n;
        while (num != 0) {
            int d = num % 10;
            if (d == i)
                count++;
            num = num / 10;
        }
        if (count > 1)
            return false;  // not a unique number
    }
    return true;  // unique number
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;

    if (isUnique(n)) {
        cout << n << " is a Unique Number." << endl;
    } else {
        cout << n << " is not a Unique Number." << endl;
    }

    return 0;
}



Time Complexity:

  • Inner Loop Complexity:
    • For each digit from 0 to 9, the inner loop processes the number n by repeatedly dividing it by 10 (to extract digits). This loop runs for a maximum of O(d) iterations, where d is the number of digits in the number n.
    • In the worst case, the number n could have up to O(log n) digits (since a number n has roughly log n digits in base 10).
  • Outer Loop Complexity:
    • The outer loop runs 10 times (for each digit from 0 to 9).

Thus, the total time complexity is:

  • Time Complexity: O(10 * d) = O(d), where d is the number of digits in the number n. Since d is proportional to log n, the overall time complexity is O(log n).

Space Complexity:

  • The function uses only a few integer variables to count occurrences and to manipulate the number n.

  • No additional data structures (such as arrays or hash maps) are used to store values.

  • Therefore, the space complexity is constant.

  • Space Complexity: O(1).

Summary: 

  • Time Complexity: O(log n) (proportional to the number of digits in the number n).
  • Space Complexity: O(1) (constant space usage).

Thank You for reading this post. You can check out more articles like this on www.physicswallah.in . 

If you have any doubts then you can post a comment below.

No comments:

Post a Comment

 

Notes Centre is an independent educational website. It is not affiliated with, endorsed by or connected to Physics Wallah Ltd, NCERT, CBSE, AKTU or any company named on this site. About · Disclaimer · Privacy · Editorial policy · Copyright · Contact