-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapsExample.java
More file actions
41 lines (29 loc) · 812 Bytes
/
HashMapsExample.java
File metadata and controls
41 lines (29 loc) · 812 Bytes
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
package DataStructures;
import java.util.HashMap;
import java.util.Map;
public class HashMapsExample {
Map<Character, Integer> map = new HashMap<>();
public Character findFirstNonRepeating(String input) {
for (char ch : input.toCharArray()) {
if (ch == ' ') {
continue;
} else if (!map.containsKey(ch)) {
map.put(ch, 1);
} else {
map.put(ch, map.get(ch) + 1);
}
}
for (char ch : input.toCharArray()) {
if (ch == ' ' || map.get(ch) > 1) {
continue;
} else {
return ch;
}
}
return '0';
}
@Override
public String toString() {
return "HashMaps [map=" + map + "]";
}
}