Here's an example Java program that gets the distinct characters and their count in a string:
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.
The code given below finds all the distinct characters and their count in a string using Java:
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
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Here's an example Java program that gets the distinct characters and their count in a string:
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.
The code given below finds all the distinct characters and their count in a string using Java: