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 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
0to9, the inner loop processes the numbernby repeatedly dividing it by 10 (to extract digits). This loop runs for a maximum ofO(d)iterations, wheredis the number of digits in the numbern. - In the worst case, the number
ncould have up toO(log n)digits (since a numbernhas roughlylog ndigits in base 10).
- For each digit from
- 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), wheredis the number of digits in the numbern. Sincedis proportional tolog n, the overall time complexity isO(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 numbern). - 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.
.png)
No comments:
Post a Comment