-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFIndCharRepetetion.java
More file actions
71 lines (55 loc) · 1.63 KB
/
Copy pathFIndCharRepetetion.java
File metadata and controls
71 lines (55 loc) · 1.63 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
63
64
65
66
67
68
69
70
71
package string;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
public class FIndCharRepetetion {
public static void main(String[] args) {
String name = "koushiK";
// System.out.println(name.compareTo("K"));
// usingStreams(name);
usingMap(name);
// withOutUsingMap(name);
}
private static void withOutUsingMap(String name) {
name = name.replaceAll("\\s", "").toUpperCase();
int len = name.length();
while (len > 0) {
int count = 1;
for (int j = 1; j < len; j++) {
if (name.charAt(0) == name.charAt(j)) {
count++;
}
}
if (count > 1) {
System.out.println(name.charAt(0) + " : " + count);
}
String character = String.valueOf(name.charAt(0)).trim();
name = name.replaceAll(character, "");
len -= count;
}
}
private static void usingMap(String name) {
char[] charArray = name.toCharArray();
Map<Character, Integer> map = new LinkedHashMap<>();
for (char c : charArray) {
System.out.println((int) c);
boolean containsKey = map.containsKey(c)
|| map.containsKey((c) + 32);
if (containsKey) {
Integer integer = map.get(c);
map.put(c, integer + 1);
} else
map.put(c, 1);
}
for (Entry<Character, Integer> c : map.entrySet()) {
System.out.println(c.getKey() + "-->" + c.getValue());
}
}
private static void usingStreams(String name) {
Map<String, Long> map = Arrays.stream(name.split(""))
.collect(Collectors.groupingBy(g -> g, Collectors.counting()));
System.out.println(map);
}
}