Skip to content

Commit 249d0a8

Browse files
find duplicate characters in a String?
find duplicate characters in a String? (solution) 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
1 parent 0b21700 commit 249d0a8

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Challenge06.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package challenge06;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
import org.junit.Test;
7+
8+
/**
9+
* The Class Challenge06.
10+
*
11+
* find duplicate characters in a String.
12+
* You need to write a program to print all duplicate character
13+
* and their count in Java. For example if given String
14+
* is "Programming" then your program should print
15+
* g : 2
16+
* r : 2
17+
* m : 2
18+
*/
19+
public class Challenge06 {
20+
21+
public static void printduplcatesWithCount(String input) {
22+
Map<Character, Integer> counter=new LinkedHashMap<Character, Integer>();
23+
for(int i=0;i<input.length()-1;i++) {
24+
Integer oldvalue=counter.put(input.charAt(i), 1);
25+
if(oldvalue!=null) {
26+
counter.put(input.charAt(i), oldvalue+1);
27+
}
28+
}
29+
30+
counter.forEach((k,v)-> System.out.println(k +" :" + v));
31+
32+
33+
}
34+
35+
@Test
36+
public void test() {
37+
Challenge06.printduplcatesWithCount("{Programming");
38+
}
39+
}

0 commit comments

Comments
 (0)