write a java program to find the duplicate characters in a string: Here’s a Java program that finds the duplicate characters in a given string:
import java.util.HashMap;
import java.util.Map;
public class DuplicateCharacters {
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 ch = str.charAt(i);
// check if the character is already in the map
if (charCountMap.containsKey(ch)) {
// increment the count for the character
int count = charCountMap.get(ch);
charCountMap.put(ch, count + 1);
} else {
// add the character to the map with a count of 1
charCountMap.put(ch, 1);
}
}
// iterate through the map and print the duplicate characters
for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey() + " occurs " + entry.getValue() + " times.");
}
}
}
}
This program uses a HashMap
to store the count of each character in the string. It iterates through each character in the string, checks if it’s already in the map, and increments the count if it is. If it’s not already in the map, it adds the character with a count of 1. Finally, it iterates through the map and prints the characters that have a count greater than 1, indicating that they are duplicates.
- What is Abstract Class in Java
- What is Lambda Expression in Java 8
- What is Exception Handling in Java
- Types of Exceptions in Java
- What is the Final Keyword in Java
- What is Wrapper Class in Java
- Break Keywords in Java with Example
- Types of access specifiers in java
- Explain File Handling in Java with Program
String important Coding Questions
Here are some important coding questions related to strings in Java:
- Write a Java program to reverse a string.
- Write a Java program to check if two strings are anagrams.
- Write a Java program to check if a string is a palindrome.
- Write a Java program to find the first non-repeated character in a string.
- Write a Java program to remove all whitespace from a string.
- Write a Java program to count the number of vowels in a string.
- Write a Java program to find the longest substring without repeating characters.
- Write a Java program to find the second most frequent character in a string.
- Write a Java program to find the number of occurrences of a given character in a string.
- Write a Java program to convert a string to an integer.
These are just a few examples of the types of string-related coding questions you may encounter in Java interviews or coding challenges. It’s important to have a solid understanding of string manipulation methods and techniques in Java in order to effectively solve these types of problems.
- Write a Java program to find the length of the longest palindrome in a string.
- Write a Java program to find the first non-repeating character in a string.
- Write a Java program to count the number of words in a string.
- Write a Java program to find the maximum occurring character in a string.
- Write a Java program to check if a string is a rotation of another string.
- Write a Java program to check if a string contains only digits.
String Programming interview questions in java for experienced
- Write a program to reverse a given string in Java.
- Implement a function that checks whether a given string is a palindrome or not.
- Write a function to check whether two given strings are anagrams or not.
- Implement a function that removes all the duplicates from a given string and returns the unique characters.
- Write a program to find the first non-repeated character in a given string.
- Implement a function that checks whether a given string contains only digits or not.
- Write a program to find the longest common prefix in an array of strings.
- Implement a function to check whether a given string is a valid email address or not.
- Write a program to find the most frequent word in a given string.
- Implement a function that returns the second most frequent character in a given string.