Find duplicates characters in string in Java

There are several ways to find duplicate characters in a string in Java. Here are a few examples:


  1. 1. Using a nested loop:
  2. public class DuplicateCharacters {
        public static void main(String[] args) {
            String str = "hello world";
            int count;
    
            // Convert the string to lowercase to make the comparison case-insensitive
            str = str.toLowerCase();
    
            // Convert the string to a char array
            char[] charArray = str.toCharArray();
    
            System.out.println("Duplicate characters in the string: ");
    
            // Iterate over each character in the string
            for (int i = 0; i < charArray.length; i++) {
                count = 1;
                // Compare each character with the remaining characters in the string
                for (int j = i + 1; j < charArray.length; j++) {
                    if (charArray[i] == charArray[j] && charArray[i] != ' ') {
                        count++;
                        // Set the character to '\0' to mark it as visited and avoid counting it again
                        charArray[j] = '\0';
                    }
                }
                // If a character is found to be a duplicate, print it
                if (count > 1 && charArray[i] != '\0')
                    System.out.println(charArray[i]);
            }
        }
    }
  1. 2. Using a HashMap:

  2. import java.util.HashMap;
    import java.util.Map;
    
    public class DuplicateCharacters {
        public static void main(String[] args) {
            String str = "hello world";
            str = str.toLowerCase();
            char[] charArray = str.toCharArray();
    
            Map<Character, Integer> charCountMap = new HashMap<>();
    
            for (char c : charArray) {
                if (charCountMap.containsKey(c)) {
                    charCountMap.put(c, charCountMap.get(c) + 1);
                } else {
                    charCountMap.put(c, 1);
                }
            }
    
            System.out.println("Duplicate characters in the string: ");
    
            for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
                if (entry.getValue() > 1 && entry.getKey() != ' ') {
                    System.out.println(entry.getKey());
                }
            }
        }
    }
  1. 3. Using Java 8 Streams:

  2. public class DuplicateCharacters {
        public static void main(String[] args) {
            String str = "hello world";
            str = str.toLowerCase();
            str.chars()
                    .mapToObj(c -> (char) c)
                    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                    .entrySet()
                    .stream()
                    .filter(e -> e.getValue() > 1 && e.getKey() != ' ')
                    .forEach(e -> System.out.println(e.getKey()));
        }
    }


All three of these programs produce the same output, which is a list of the duplicate characters in the input string:


Duplicate characters in the string:
l
o