Find number of occurrence of each of the unique characters in a string ?
- In this program an approach using Hashmap in Java.
- Declare a Hashmap in Java of {char, int}.
- Traverse in the string, check if the Hashmap already contains the traversed character or not.
- If it is present, then increase its count using get() and put() function in Hashmap.
- Once the traversal is completed, traverse in the Hashmap and print the character and its frequency.
Example
Input: str = "Wikitechy"
Output:
W 1
i 2
k 1
t 1
e 1
c 1
h 1
y 1
Sample Code in Java
// Java prorgam to count frequencies of
// characters in string using Hashmap
import java.io.*;
import java.util.*;
class OccurenceOfCharInString {
static void characterCount(String inputString)
{
// Creating a HashMap containing char
// as a key and occurrences as a value
HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
// Converting given string to char array
char[] strArray = inputString.toCharArray();
// checking each char of strArray
for (char c : strArray) {
if (charCountMap.containsKey(c)) {
// If char is present in charCountMap,
// incrementing it's count by 1
charCountMap.put(c, charCountMap.get(c) + 1);
}
else {
// If char is not present in charCountMap,
// putting this char to charCountMap with 1 as it's value
charCountMap.put(c, 1);
}
}
// Printing the charCountMap
for (Map.Entry entry : charCountMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
// Driver Code
public static void main(String[] args)
{
String str = "Wikitechy";
characterCount(str);
}
}
Output
c 1
t 1
e 1
W 1
h 1
i 2
y 1
k 1
Categorized in:
Tagged in:
Accenture interview questions and answers, Applied Materials interview questions and answers, Atos interview questions and answers, Capgemini interview questions and answers, CASTING NETWORKS INDIA PVT LIMITED interview questions and answers, CGI Group Inc interview questions and answers, charat java, Chetu interview questions and answers, Ciena Corporation interview questions and answers, Collabera Technologies interview questions and answers, compro technologies interview questions and answers, count occurrences of character in string java using hashmap, Dell International Services India Pvt Ltd interview questions and answers, FIS Global Business Solutions India Pvt Ltd interview questions and answers, Flipkart interview questions and answers, how to count characters in string array in java, how to count occurrence of a given character in a string in c, IBM interview questions and answers, Indecomm Global Services interview questions and answers, Infosys Technologies interview questions and answers, java counter, java replace, java string, java string compare, java string contains, java string length, java string replace, java trim, L&T Infotech interview questions and answers, Mphasis interview questions and answers, NetApp interview questions and answers, occurrence of each character in string java, Oracle Corporation interview questions and answers, PeopleStrong interview questions and answers, Persistent Systems interview questions and answers, program to find occurrence of a character in a string in java, RBS India De interview questions and answers, Reliance Industries Ltd interview questions and answers, SAP Labs India Pvt Ltd interview questions and answers, string class in java, string class methods in java, string count, substring java, Tech Mahindra interview questions and answers, UnitedHealth Group interview questions and answers, Virtusa Consulting Services Pvt Ltd interview questions and answers, Wells Fargo interview questions and answers, Wipro Infotech interview questions and answers