---
title: "Write a Java program to get distinct characters and their count in a string."  
description: "Write a Java program to get distinct characters and their count in a string."  
author: "Ashutosh Patel"  
published: 2023-03-22  
updated: 2023-04-28  
canonical: https://www.mindstick.com/forum/157545/write-a-java-program-to-get-distinct-characters-and-their-count-in-a-string  
category: "java"  
tags: ["java", "programs"]  
reading_time: 3 minutes  

---

# Write a Java program to get distinct characters and their count in a string.

Write a [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to get [distinct](https://www.mindstick.com/forum/159558/why-c-sharp-linq-distinct-doesn-t-work) [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-characters) and their [count](https://www.mindstick.com/forum/157774/selecting-count-with-distinct) in a string.

## Replies

### Reply by Aryan Kumar

Here's an example Java program that gets the distinct characters and their count in a string:

```java
import java.util.HashMap;
import java.util.Map;
public class DistinctCharactersCount {
   public static void main(String[] args) {
       String str = "Hello, World!";
       Map<Character, Integer> charCountMap = new HashMap<>();
       // Iterate through each character in the string
       for (int i = 0; i < str.length(); i++) {
           char c = str.charAt(i);
           // If the character is already in the map, increment its count
           if (charCountMap.containsKey(c)) {
               charCountMap.put(c, charCountMap.get(c) + 1);
           }
           // Otherwise, add the character to the map with a count of 1
           else {
               charCountMap.put(c, 1);
           }
       }
       // Print the distinct characters and their count
       System.out.println("Distinct characters in the string '" + str + "':");
       for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
           System.out.println("'" + entry.getKey() + "' : " + entry.getValue());
       }
   }
}
```

In this example, we first define a string called **str** that we want to count the distinct characters in. We then create a **HashMap** called **charCountMap** to store the count of each distinct character in the string.

Next, we iterate through each character in the string using a for loop. For each character, we check if it already exists in the **charCountMap**. If it does, we increment its count by 1. If it doesn't, we add the character to the map with a count of 1.

Finally, we print out the distinct characters and their count using a for-each loop to iterate over the entries in the **charCountMap**.

### Reply by Krishnapriya Rajeev

The code given below finds all the distinct characters and their count in a string using Java:

```plaintext
public class trial
{
    public static void main(String args[])
    {
        String str = "Welcome to MindStick";	// Declaration and initialization of string
        int MAX = 256;		// Maximum number of possible characters
        int count[] = new int[MAX];		// Stores count of each character

        int len = str.length();

        for (int i = 0; i < len; i++)
         count[str.charAt(i)]++;	// Increments count of each character by 1

        char ch[] = new char[str.length()];

        for (int i = 0; i < len; i++)
        {
            ch[i] = str.charAt(i);
            int find = 0;
            for (int j = 0; j <= i; j++)
            {
                if (str.charAt(i) == ch[j])
                find++;
            }
            if (find == 1)		// If first occurence of character
            System.out.println("The frequency of "+ str.charAt(i)+ " is: " + count[str.charAt(i)]);
        }
    }
}
OUTPUT:
The frequency of W is: 1
The frequency of e is: 2
The frequency of l is: 1
The frequency of c is: 2
The frequency of o is: 2
The frequency of m is: 1
The frequency of   is: 2
The frequency of t is: 2
The frequency of M is: 1
The frequency of i is: 2
The frequency of n is: 1
The frequency of d is: 1
The frequency of S is: 1
The frequency of k is: 1
```


---

Original Source: https://www.mindstick.com/forum/157545/write-a-java-program-to-get-distinct-characters-and-their-count-in-a-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
