Write a java Program to find the duplicate Characters in a String

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.

String important Coding Questions

Here are some important coding questions related to strings in Java:

  1. Write a Java program to reverse a string.
  2. Write a Java program to check if two strings are anagrams.
  3. Write a Java program to check if a string is a palindrome.
  4. Write a Java program to find the first non-repeated character in a string.
  5. Write a Java program to remove all whitespace from a string.
  6. Write a Java program to count the number of vowels in a string.
  7. Write a Java program to find the longest substring without repeating characters.
  8. Write a Java program to find the second most frequent character in a string.
  9. Write a Java program to find the number of occurrences of a given character in a string.
  10. 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

  1. Write a program to reverse a given string in Java.
  2. Implement a function that checks whether a given string is a palindrome or not.
  3. Write a function to check whether two given strings are anagrams or not.
  4. Implement a function that removes all the duplicates from a given string and returns the unique characters.
  5. Write a program to find the first non-repeated character in a given string.
  6. Implement a function that checks whether a given string contains only digits or not.
  7. Write a program to find the longest common prefix in an array of strings.
  8. Implement a function to check whether a given string is a valid email address or not.
  9. Write a program to find the most frequent word in a given string.
  10. Implement a function that returns the second most frequent character in a given string.

Leave a Comment