- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to check occurrence of each character in String
In order to find occurence of each character in a string we can use Map utility of Java.In Map a key could not be duplicate so make each character of string as key of Map and provide initial value corresponding to each key as 1 if this character does not inserted in map before.Now when a character repeats during insertion as key in Map increase its value by one.Continue this for each character untill all characters of string get inserted.
Example
public class occurenceOfCharacter { public static void main(String[] args) { String str = "SSDRRRTTYYTYTR"; HashMap <Character, Integer> hMap = new HashMap<>(); for (int i = str.length() - 1; i > = 0; i--) { if (hMap.containsKey(str.charAt(i))) { int count = hMap.get(str.charAt(i)); hMap.put(str.charAt(i), ++count); } else { hMap.put(str.charAt(i),1); } } System.out.println(hMap); } }
Output
{D=1, T=4, S=2, R=4, Y=3}
- Related Articles
- Java program to check occurrence of each vowel in String
- Java program to check occurence of each character in String
- Java program to count the occurrence of each character in a string using Hashmap
- Python program to find occurrence to each character in given string
- C Program to find maximum occurrence of character in a string
- C Program to find minimum occurrence of character in a string
- Java Program to Iterate through each character of the string.
- C program to replace all occurrence of a character in a string
- C# Program to find number of occurrence of a character in a String
- Java program to check occurence of each vowel in String
- Finding the last occurrence of a character in a String in Java
- Java Program to Capitalize the first character of each word in a String
- Java program to check if a string contains any special character
- String function to replace nth occurrence of a character in a string JavaScript
- Replace first occurrence of a character in Java

Advertisements