The problem statement say that we need to identify how many time a character appear in a string.
Program :
package net.jubiliation.example;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class CharacterFrequencyExample {
public static void main(String[] args) {
String str = "yweeeeerwwesfsfsfsgsgdgxcbgd";
// convert this String into array of character
char[] characterArray = str.toCharArray();
final Map<Character, Integer> charactersMap = new HashMap<>();
// maintain for each loop to traverse through each element
for (Character character : characterArray) {
if (charactersMap.containsKey(character)) {
int count = charactersMap.get(character);
count++;
charactersMap.put(character, count);
} else {
charactersMap.put(character, 0);
}
}
// Iterating the map to print the characters and frequency
for (Entry<Character, Integer> characterEntry : charactersMap.entrySet()) {
Character character = characterEntry.getKey();
if (charactersMap.containsKey(character)) {
int count = charactersMap.get(character);
count++;
charactersMap.put(character, count);
if (count > 1) {
System.out.println("Duplicate Character: " + character);
}
System.out.println("Character: " + character + " | Frequency: " + count);
}
}
}
}
Result :
Character: r | Frequency: 1
Duplicate Character: s
Character: s | Frequency: 4
Duplicate Character: e
Character: e | Frequency: 6
Duplicate Character: f
Character: f | Frequency: 3
Duplicate Character: w
Character: w | Frequency: 3
Character: y | Frequency: 1
Program :
package net.jubiliation.example;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class CharacterFrequencyExample {
public static void main(String[] args) {
String str = "yweeeeerwwesfsfsfsgsgdgxcbgd";
// convert this String into array of character
char[] characterArray = str.toCharArray();
final Map<Character, Integer> charactersMap = new HashMap<>();
// maintain for each loop to traverse through each element
for (Character character : characterArray) {
if (charactersMap.containsKey(character)) {
int count = charactersMap.get(character);
count++;
charactersMap.put(character, count);
} else {
charactersMap.put(character, 0);
}
}
// Iterating the map to print the characters and frequency
for (Entry<Character, Integer> characterEntry : charactersMap.entrySet()) {
Character character = characterEntry.getKey();
if (charactersMap.containsKey(character)) {
int count = charactersMap.get(character);
count++;
charactersMap.put(character, count);
if (count > 1) {
System.out.println("Duplicate Character: " + character);
}
System.out.println("Character: " + character + " | Frequency: " + count);
}
}
}
}
Result :
Character: r | Frequency: 1
Duplicate Character: s
Character: s | Frequency: 4
Duplicate Character: e
Character: e | Frequency: 6
Duplicate Character: f
Character: f | Frequency: 3
Duplicate Character: w
Character: w | Frequency: 3
Character: y | Frequency: 1
Very interesting post! Thanks for sharing this amazing post for the statement problem that we need to identify easily. I would like to share with my friends.
ReplyDeleteVisit Here: Ashvin Ranpariya
Some of these points are really workable, good post, thanks for share
ReplyDeleteI admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. Read more info about Data Structures In C++ Online Course
ReplyDeleteits very effective blog for trading Tata Steel Share Price
ReplyDelete