forked from cybernobie/Cognizant_Early_Engagement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniqueChar.java
48 lines (38 loc) · 1.25 KB
/
UniqueChar.java
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
import java.util.*;
class UniqueChar {
private static boolean printUnique(String sentence) {
char[] chars = sentence.toCharArray();
Map<Character, Integer> map = new LinkedHashMap<>();
for (char ch : chars) {
if (Character.isDigit(ch)) {
return false;
} else if (!Character.isWhitespace(ch)) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
}
List<Character> uniqueCharacters = new ArrayList<>();
for (char key : map.keySet()) {
if (map.get(key) == 1) {
uniqueCharacters.add(key);
}
}
if (uniqueCharacters.isEmpty()) {
System.out.println("No unique characters");
} else {
System.out.println("Unique characters:");
for (char ch : uniqueCharacters) {
System.out.println(ch);
}
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String sentence;
System.out.println("Enter the sentence:");
sentence = scanner.nextLine();
if (!printUnique(sentence)) {
System.out.println("Invalid Sentence");
}
}
}