-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateCharacter.java
More file actions
35 lines (30 loc) · 985 Bytes
/
Copy pathDuplicateCharacter.java
File metadata and controls
35 lines (30 loc) · 985 Bytes
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
import java.util.Scanner;
public class DuplicateCharacter {
static void checkDuplicates(char c[]){
boolean visited[]= new boolean[c.length];
for(int i=0; i<c.length; i++){
if (visited[i]== false){
int count= 0;
for(int j=i+1; j<c.length; j++){
if(c[i]==c[j]){
visited[j]= true;
count++;
System.out.print(j + " ");
}
}
if(count>0){
System.out.println("," + c[i] + " duplicates " + count + " times");
}
}
}
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
//STRING INPUT
System.out.print("Enter a string: ");
String s= sc.nextLine();
//CONVERT THE STRING TO CHAR ARRAY
char c[]= s.toCharArray();
checkDuplicates(c);
}
}