Find frequency of characters using Hashmap

Find frequency of characters using Hashmap

Question

Given a word, find the frequency of each character in the word. Get the input from the user and output must be as follows

 

Logic

  • To find frequency of characters, check each character, if it comes first time set the count of that character to 1.
  • If any character occurs more than once increment the count accordingly.
  • Finally, print the count of each character.

Algorithm

  1. Create a Hashmap. In hashmap, we can store key and values. For each key, there will be one value. See this for more details related to the hashmap
  2. Here we treat a character as key and count as its value.
  3. If we see a character the first time and insert the key and set value 1 ( ex: ‘a’ is key and value is 1)
  4. If the character is already present in map increase the value by one.
  5. Finally, convert the map to set using entryset() function and print frequency of characters.

Program

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

class Frequency
{
    public static void main(String ar[])
    {
        String in;
        int temp;
        Scanner sc = new Scanner(System.in);
        System.out.println("enter string");
        in = sc.next();                             //get input word
        Map<Character,Integer> map= new HashMap<>();
        char[] arr = in.toCharArray();              //convert string to character array
        int length = arr.length;
        System.out.println(length);
        for(int i=0;i<length;i++)
        {
            if(!map.containsKey(arr[i]))            //if key not present in map
            {
                    map.put(arr[i], 1);             //insert count 1 for characer arr[i]
                    
            }
            else
            {
               temp= map.get(arr[i));               //get count of the character
               temp++;
               map.put(arr[i], temp);               //add incemented count
               
            }
            
        }
        /*converting map to equivalent set*/
       System.out.println("The frequency is "+map.entrySet());
       
       /*iterating over map using keys*/
       Set<Character> keys = map.keySet();
       for(Character key : keys)
       {
           System.out.println("Key :"+ key+"Value :"+map.get(key));
       }
            
    }
}

Analysis

worst case – O(n) n is the length of the word and we check character one by one so complexity is of order n.

You might also like…

Smallest alphabet greater than given character

Program to print all the substrings using java

 

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Sree Hari Sanjeev

The founder of Wisdom Overflow. Software Developer at Zoho Corporation.
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x