File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public class DuplicateCharacters {
2+ public static void main(String[] args) {
3+ String string1 = "Great responsibility";
4+ int count;
5+
6+ //Converts given string into character array
7+ char string[] = string1.toCharArray();
8+
9+ System.out.println("Duplicate characters in a given string: ");
10+ //Counts each character present in the string
11+ for(int i = 0; i <string.length; i++) {
12+ count = 1;
13+ for(int j = i+1; j <string.length; j++) {
14+ if(string[i] == string[j] && string[i] != ' ') {
15+ count++; //Set string[j] to 0 to avoid printing visited character
16+
17+ string[j] = '0';
18+ }
19+ }
20+ //A character is considered as duplicate if count is greater than 1
21+ if(count > 1 && string[i] != '0')
22+ System.out.println(string[i]);
23+ }
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments