-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharArray.java
More file actions
46 lines (33 loc) · 957 Bytes
/
CharArray.java
File metadata and controls
46 lines (33 loc) · 957 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
42
43
44
45
46
package com.stubhub.interviews.tests;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class CharArray {
public static void main(String[] args) {
String[] str = {"abc","acd","aabb"};
charArray(str);
}
public static void charArray(String[] A){
char[] ch;
TreeMap<Character, Integer> count = new TreeMap<Character, Integer>();
for(int i=0;i<A.length;i++){
ch = A[i].toCharArray();
for(int j=0;j<ch.length;j++){
if(count.containsKey(ch[j])){
int inc = count.get(ch[j]) + 1;
count.put(ch[j], inc);
}
else{
count.put(ch[j], 1);
}
}
}
Iterator<Map.Entry<Character, Integer>> entries = count.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Character, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
}