➤ Sum of digits in a String
➤ Count No of Vowels String
➤ String Pattern Programs in Java
➤ Take String Input In Java
➤ Take Multiple String Input in Java
➤ How To Reverse a String In Java
➤ Remove Special Characters
➤ String – Remove Character
➤ String Palindrome In Java
➤ Sort String In Java
➤ How to Compare Strings In Java
➤ Second Occurrence of Character
➤ Replace nth Occurrence String
➤ Last Occurrence of Character
➤ Uppercase & Lowercase
➤ Java Check If Char is Uppercase
➤ Check If String is Uppercase
➤ Swap Characters in String Java
➤ Java String indexOf() Method
➤ How to Replace Dot in Java?
➤ How to Find Length of String
➤ Substring Method In Java
➤ Split Method In Java
How To Find Repeated Characters In A String In Java | How To Find Repeated Characters In A String In Java? How To Count Repeated Characters In A String Java? In this blog we will see how to find repeated characters in a string in Java, and if repeated characters are available then how to count repeated characters in a string in Java?
We can solve this problem very easily with the help of a Map. The map is used to hold the key and value pair. The idea is to iterate each character of the string. For each character put an entry in the key, and count the number of occurrences of each character. The number of occurrences will be updated in the value of the map. Finally, we will have a map containing characters as keys and the number of occurrences as values.
Example:- “Hello World”
Character | Count |
---|---|
‘H’ | 1 |
‘e’ | 1 |
‘l’ | 3 |
‘o’ | 2 |
' ' | 1 |
‘W’ | 1 |
‘r’ | 1 |
‘d’ | 1 |
Find & Count Repeated Characters In A String In Java
The characters which occurred more than once are repeated. To count repeated characters in a string we can iterate the map and check the occurrences.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter string: ");
String string = scan.nextLine();
Map<Character, Integer> chars = new HashMap<>();
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
if (chars.containsKey(ch)) {
chars.put(ch, chars.get(ch) + 1);
} else {
chars.put(ch, 1);
}
}
System.out.println("Chars: " + chars);
List<Character> repeatedChars = new ArrayList<>();
for (Map.Entry<Character, Integer> map : chars.entrySet()) {
if (map.getValue() > 1) {
repeatedChars.add(map.getKey());
}
}
System.out.println("Count of repeated characters: " + repeatedChars.size());
System.out.println("Repeated characters: " + repeatedChars);
scan.close();
}
}
Output:-
Enter string: Hello World!
Chars: { =1, !=1, r=1, d=1, e=1, W=1, H=1, l=3, o=2}
Count of repeated characters: 2
Repeated characters: [l, o]
Enter string: I love programming.
Chars: { =2, a=1, e=1, g=2, I=1, i=1, l=1, m=2, n=1, .=1, o=2, p=1, r=2, v=1}
Count of repeated characters: 5
Repeated characters: [ , g, m, o, r]
Enter string: Sometimes life is going to hit you in the head with a brick. Don’t lose faith.
Chars: {D=1, S=1, ’=1, =15, a=3, b=1, c=1, d=1, e=6, f=2, g=2, h=5, i=9, k=1, l=2, m=2, n=3, .=2, o=6, r=1, s=3, t=7, u=1, w=1, y=1}
Count of repeated characters: 14
Repeated characters: [ , a, e, f, g, h, i, l, m, n, ., o, s, t]
Find & Count Repeated Characters In A String In Java Ignoring Case
Note that Java is case sensitive programming language. Therefore it treats lowercase and uppercase differently. Hence ‘D’ and ‘d’ are different and in the string ‘D’ or ‘d’ is not repeated. If we want to ignore the case then we have to convert the string/character either into lowercase or uppercase.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter string: ");
String string = scan.nextLine().toLowerCase();
Map<Character, Integer> chars = new HashMap<>();
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
if (chars.containsKey(ch)) {
chars.put(ch, chars.get(ch) + 1);
} else {
chars.put(ch, 1);
}
}
System.out.println("Chars: " + chars);
System.out.println("Total characters: " + string.length());
System.out.println("Unique characters (ignoring case): " + chars.size());
List<Character> repeatedChars = new ArrayList<>();
for (Map.Entry<Character, Integer> map : chars.entrySet()) {
if (map.getValue() > 1) {
repeatedChars.add(map.getKey());
}
}
System.out.println("Count of repeated characters: " + repeatedChars.size());
System.out.println("Repeated characters: " + repeatedChars);
scan.close();
}
}
Output:-
Enter string: Sometimes life is going to hit you in the head with a brick. Don’t lose faith.
Chars: { =15, a=3, b=1, c=1, d=2, e=6, f=2, g=2, h=5, i=9, k=1, l=2, m=2, n=3, .=2, o=6, r=1, s=4, t=7, u=1, w=1, y=1, ’=1}
Total characters: 78
Unique characters (ignoring case): 23
Count of repeated characters: 15
Repeated characters: [ , a, d, e, f, g, h, i, l, m, n, ., o, s, t]
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!