-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintduplicates.java
More file actions
54 lines (48 loc) · 1.56 KB
/
printduplicates.java
File metadata and controls
54 lines (48 loc) · 1.56 KB
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
47
48
49
50
51
52
53
54
//prinyt repeated characters
import java.util.Arrays;
public class printduplicates {
public static void printDuplicates(String str)
{
int len = str.length();
// Sorting the string
char[] chars = str.toCharArray();
Arrays.sort(chars);
String sortedStr = new String(chars);
// Loop through the sorted string to find duplicates
for (int i = 0; i < len; i++) {
int count = 1;
// Counting the occurrences of each character
while (i < len - 1
&& sortedStr.charAt(i)
== sortedStr.charAt(i + 1)) {
count++;
i++;
}
// Printing the duplicate character and its
// count
if (count > 1) {
System.out.println(sortedStr.charAt(i)
+ ", count = " + count);
}
}
}
public static void main(String[] args)
{
String str = "aaabbcdddds";
printDuplicates(str);
}
/*public static void main(String argu[]) {
String str = "beautiful beach";
char[] carray = str.toCharArray();
System.out.println("The string is:" + str);
System.out.print("Duplicate Characters in above string are: ");
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
if (carray[i] == carray[j]) {
System.out.print(carray[j] + " ");
break;
}
}
}
}*/
}