-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicates_InWord.java
More file actions
26 lines (25 loc) · 1.03 KB
/
Duplicates_InWord.java
File metadata and controls
26 lines (25 loc) · 1.03 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
// WAP to find the duplicate character in a String/word
import java.util.Scanner;
public class Duplicates_InWord {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter word: ");
String str = sc.nextLine();
sc.close();
str = str.toLowerCase(); // to remove java case sensitiveness property
int[] charCounts = new int[256];
System.out.print("Duplicate Character are: ");
boolean foundDuplicates = false;
for (int i = 0; i < str.length(); i++) {
charCounts[str.charAt(i)]++; // Increment the count for each element and store it in the array
// If the count becomes 2, it's the first time we've detected this duplicate
if (charCounts[str.charAt(i)] == 2) {
System.out.print(str.charAt(i) + " ");
foundDuplicates = true;
}
}
if(!foundDuplicates){
System.out.println("None");
}
}
}