Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

How to get distinct characters and their count in a string

Sumit Sharma

In this shot, we will discuss how we can get distinct characters and their frequency in a given string in Java.

Solution approach

In order to get the distinct element and their count in a string in Java, we will use the Java HashMap. We will convert the string into uppercase or lowercase for our convenience.

We will traverse through the given string using a loop and for each character of the string we will check if the map contains this character. If it does not contains the same character, then we will put that character into map with frequency 1. Otherwise we will increase the frequency of that element in the map.

HashMap

HashMap is a part of Java Collections and frameworks. It stores objects in key-value form where every key is distinct. In this code, we will be using some library methods of HashMap that is provided by Java. They are as follows:

  • .put(key,value): It stores the key and respective value in the map.
  • .get(key): It returns the value of the given key.

Example

Let’s take a string “JAVA”. Here, distinct character and their count are:

  • J -> 1
  • A -> 2
  • V -> 1

All the elements here will be printed in lexicographical order. By using the same approach we can also find the element with maximum or minimum frequency.

Code

Let’s see the code snippet.

import java.util.*;
 
class Solution1 {
    public static void main(String[] args) {
        Scanner sc= new Scanner((System.in));
        System.out.println("Enter the input String:-");
        String input= sc.nextLine();
        input=input.toUpperCase();
        HashMap<Character,Integer> map= new HashMap<>();
        /*
        //Traverse through the String and put each
        // distinct character into the HashMap with frequency 1.
        //if any character is already present in hashmap
        //add 1 to it's freuency */
 
        for(char c:input.toCharArray()){
            if(!map.containsKey(c))
            {
                map.put(c,1);
            }else{
                int freq= map.get(c);
                map.put(c,freq+1);
            }
        }
        for(Map.Entry<Character,Integer> e:map.entrySet()){
            System.out.println(e.getKey()+" -> "+e.getValue());
        }
    }
 
}

Enter the input below

Implementing HashMap function to count distinct characters in a String

Explanation

  • In line 1 we import the java.util.* to use Java built-in methods.

  • In line 7, inside the main function, we have taken the input string from the user by creating a Scanner object and stored that input in a string input.

  • In line 8 we are converting the string into uppercase letters using the toUpperCase() method.

  • In line 9 we have initialized a map of character and integer type i.e., the key of the map will be a character and the corresponding value will be of integer type so that we can store the frequency of the character.

  • In lines 16 to 23 we are using another for each loop for the input string and for each character of string we are checking if the map contains that character using the if-else condition.

  • In lines 25 to 27 we are using a for each loop in the map and printing each key and it’s corresponding value.

In this way, we can get distinct characters and their count in a string in Java.

RELATED TAGS

communitycreator
java
RELATED COURSES

View all Courses

Keep Exploring