-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindFirstNonRepeatedChar.java
More file actions
44 lines (38 loc) · 1.4 KB
/
FindFirstNonRepeatedChar.java
File metadata and controls
44 lines (38 loc) · 1.4 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
/* *****************************************************************************
* Name: Jaya Mukherjee
* Coursera User ID: xxxxx
* Last modified: October 3, 2021
**************************************************************************** */
import java.util.HashMap;
public class FindFirstNonRepeatedChar {
public FindFirstNonRepeatedChar(String str) {
HashMap<Integer, Object> charhash = new HashMap<Integer, Object>();
Object seenOnce = new Object();
Object seenMul = new Object();
Object seen;
final int length = str.length();
for (int i = 0; i < length; ) {
final int cp = str.codePointAt(i);
i += Character.charCount(cp);
seen = charhash.get(cp);
if (seen == null) {
charhash.put(cp, seenOnce);
}
else if (seen.equals(seenOnce)) {
charhash.put(cp, seenMul);
}
}
for (int i = 0; i < length; ) {
final int cp = str.codePointAt(i);
i += Character.charCount(cp);
if (charhash.get(cp).equals(seenOnce)) {
System.out.println(new String(Character.toChars(cp)));
}
}
}
public static void main(String[] args) {
if (args != null) {
FindFirstNonRepeatedChar ff = new FindFirstNonRepeatedChar(args[0]);
}
}
}