-
Notifications
You must be signed in to change notification settings - Fork 1
/
Challenge06.java
39 lines (31 loc) · 932 Bytes
/
Challenge06.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
package challenge06;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
/**
* The Class Challenge06.
*
* find duplicate characters in a String.
* You need to write a program to print all duplicate character
* and their count in Java. For example if given String
* is "Programming" then your program should print
* g : 2
* r : 2
* m : 2
*/
public class Challenge06 {
public static void printduplcatesWithCount(String input) {
Map<Character, Integer> counter=new LinkedHashMap<Character, Integer>();
for(int i=0;i<input.length()-1;i++) {
Integer oldvalue=counter.put(input.charAt(i), 1);
if(oldvalue!=null) {
counter.put(input.charAt(i), oldvalue+1);
}
}
counter.forEach((k,v)-> System.out.println(k +" :" + v));
}
@Test
public void test() {
Challenge06.printduplcatesWithCount("{Programming");
}
}