Skip to content

Commit 17e3c25

Browse files
Create Print Duplicate Characters in the String
Simple java execution to print the duplicate charaters int the string
1 parent f3c25ae commit 17e3c25

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

0 commit comments

Comments
 (0)