# Find number of occurrence of each of the unique characters in a string ?

Answer : In this program an approach using Hashmap….

## 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``````

## What is Linear Sort in Java ?

Answer : One of the simplest Sorting techniques is a linear sort…

## Java program to find second largest number in an array ?

Answer : Given an array of integers, our task is to write a program…

## How to optimally divide an array into two subarrays so that sum of elements in both are same ?

Answer : Given an array of integers greater than zero…

## How to check if two rectangles intersect or not ?

Answer : The following standard Java coding practices to solve…

## What is the purpose of method overriding in java where we are completely re-defining a inherited method ?

Answer : Exception in Java represents that problem arises during the execution of the program…

## How to check if a string contains the sequences of characters or not using java ?

Answer : java.lang.String.contains() method searches the sequence of characters…