-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathUniqueNumber.java
More file actions
62 lines (52 loc) · 1.89 KB
/
UniqueNumber.java
File metadata and controls
62 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.Scanner;
/*
* This program will take in a number and check if it is unique.
* A unique number is a number that has no repeating digits.
* For example, 1234 is a unique number, but 1224 is not.
*/
public class UniqueNumber {
public static void main(String[] args) {
// Create a Scanner object to read input
Scanner input = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number: ");
// Read the number
int number = input.nextInt();
// Check if the number is unique
if (isUnique(number)) {
System.out.println(number + " is a unique number.");
} else {
System.out.println(number + " is not a unique number.");
}
}
/**
* This method will take in a number and check if it is unique.
*
* @param number The number to check if it is unique
* @return true if the number is unique, false otherwise
*/
public static boolean isUnique(int number) {
// Create an array to store the number of times each digit appears
// Digits can be from 0 to 9, so the array will have 10 elements
// The digits will represent indexes of the array
int[] array = new int[10];
// Loop through each digit of the number
while (number > 0) {
// Get the last digit of the number
int digit = number % 10;
// Increment the number of times the digit appears
array[digit]++;
// Remove the last digit from the number
number /= 10;
}
// Loop through the array
for (int j : array) {
// If a digit appears more than once, the number is not unique
if (j > 1) {
return false;
}
}
// If the loop finishes, the number is unique
return true;
}
}