$show=/label

Java Program To Count Duplicate Characters In String (+Java 8 Program)

SHARE:

A quick practical and best way to find or count the duplicate characters in a string including special characters. Example programs are shown in various java versions such as java 8, 11, 12 and Surrogate Pairs.

1. Introduction


In this article, We'll learn how to find the duplicate characters in a string using a java program. This java program can be done using many ways. But, we will focus on using the Brute-force search approach, HashMap or LinkedHashMap, Java 8 compute() and Java 8 functional style.

Time complexity will be O(n) for brute force, for others will be O(nlogn).

At last, But important to know how to handle the Unicode characters. For this problem statement solution, New Java 8 String api provided the solution with the codepoints concepts.

Let us start with the HashMap first.

Coming to the problem statement, We have to find how many times each is repeated in the input string. The input may contain special characters such as '!', '#', '@', '$'.. etc. All characters must be handled by the algorithm.

Java Program To Count Duplicate Characters In String (+Java 8 Program)




Input :

String str = "aaaabbbcccccdddddd";

Output:

a  4
b  3
c  5
d  6

2. Using a Brute-force attack


This is the simplest solution and easy to understand. We run two for loops. Here the first loop takes each character from the input string and compares with each character in the entire string in the second loop. If it finds any match then the count will be incremented by 1. The same process will be repeated for each char in the string.

Here if any character appears more than once, then it will print the same char in output twice with the same count. To avoid the same character processing again and printing in the output, Implemented another utility method named "isProcessedChar" that takes char as input and returns boolean true if already processes otherwise false.

package com.javaprogramto.w3schools.engineering.string;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @version JavaProgramTo.com
 */
public class StringCountDuplicateCharactersBruteForce {

    private static List chars = new ArrayList();

    public static void main(String[] args) {
        System.out.println("---------Input 1 ------");
        printCountOfDuplicateCharacter("JavaProgramTo.com");
        System.out.println("---------Input 2 ------");
        printCountOfDuplicateCharacter("aaaabbbcccccdddddd");

    }

    // print count of each character in a given string.
    private static void printCountOfDuplicateCharacter(String input) {

        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);

            if (isProcessedChar(ch))
                continue;

            int count = 0;
            for (int j = 0; j < input.length(); j++) {
                if (ch == input.charAt(j)) {
                    count++;
                }
            }
            System.out.println(ch + " : " + count);
        }
        chars.clear();
    }

    // checking whether the given char is already processed or not.
    private static boolean isProcessedChar(char ch) {

        if (chars.contains(Character.toString(ch))) {
            return true;
        } else {
            chars.add(Character.toString(ch));
        }
        return false;
    }

}

Output:

---------Input 1 ------
J : 1
a : 3
v : 1
P : 1
r : 2
o : 3
g : 1
m : 2
T : 1
. : 1
c : 1
---------Input 2 ------
a : 4
b : 3
c : 5
d : 6

3. Using HashMap or LinkedHashMap


HashMap takes a key-value pair and here our case, the key will be character and value will be the count of char as an integer. first, we will take a character from string and place the current char as key and value will be 1 in the map. Next, take the second character. If the char is already present in the map using containsKey() method, then simply increase the current value by 1. Repeat the same for each character.

At end print the map that holds occurrences of each character.

package com.javaprogramto.w3schools.engineering.string;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 *
 * @version JavaProgramTo.com
 */
public class StringCountDuplicateCharactersHashMap {

    public static void main(String[] args) {
        printCountOfDuplicateCharacterUsingHashMap("bbbcccccddddddaaaa");
        printCountOfDuplicateCharacterUsingHashMap("##^$!%^$!^%@!$^@!kds");

    }

    // Using hashmap : print count of each character in a given string.
    private static void printCountOfDuplicateCharacterUsingHashMap(String input) {

        Map<Character, Integer> output = new LinkedHashMap<Character, Integer>();
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (output.containsKey(ch)) {
                output.put(ch, output.get(ch) + 1);
            } else {
                output.put(ch, 1);
            }

        }

        System.out.println(output);

    }

}

Output:

In the above program, we used LinkedHashMap instead of HashMap because LinkedHashMap preserves the order of how the characters are added to the map.

The below output using LinkedHashMap.

{b=3, c=5, d=6, a=4}
{#=2, ^=4, $=3, !=4, %=2, @=2, k=1, d=1, s=1}

Change the map implementation to HashMap.

Map<Character, Integer> output = new HashMap<Character, Integer>();

HashMap Output:

{a=4, b=3, c=5, d=6}
{@=2, !=4, #=2, s=1, $=3, d=1, %=2, k=1, ^=4}

Compares both Map's output and understands how they hold the data.

4. Using Java 8 Map compute() method


Map implementations are introduced with a new method compute() in java 8 api. Now let us use the compute() method and simplify the logic of the above program.

compute() method takes key and BiFunction as arguments. BiFunction takes a key, value and returns value type value.

See the below program on how the code is reduced to a single line inside for loop using compute().

java 8 compute program:

package com.javaprogramto.w3schools.engineering.string;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Java 8 map compute() method example.
 *
 * @version JavaProgramTo.com
 */
public class StringCountDuplicateCharJava8Compute {

    public static void main(String[] args) {
        printCountOfDuplicateCharMapCompute("bbbcccccddddddaaaa");
        printCountOfDuplicateCharMapCompute("##^$!%^$!^%@!$^@!kds");

    }

    // Using hashmap : print count of each character in a given string.
    private static void printCountOfDuplicateCharMapCompute(String input) {

        Map<Character, Integer> output = new LinkedHashMap<Character, Integer>();
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);

            output.compute(ch, (key, value) -> (value == null) ? 1 : value + 1);

        }

        System.out.println(output);

    }

}

Output:

{b=3, c=5, d=6, a=4}
{#=2, ^=4, $=3, !=4, %=2, @=2, k=1, d=1, s=1}


5. Java 8 Stream API


Now, We will see how to achieve the problem using java 8 stream api concepts and its methods.

This is a three-step process. The first two steps are to transform the input string into the Stream of characters. The final step to group and count the characters.

A) Invoke the chars() method on the input string and which returns the IntStream instance. This int stream holds the integer representation of each character in the string.
B) Need to convert IntStream to Char stream using the mapToObj() method.
C) Last, need to group by characters by calling Collectors.groupingBy() and to count call Collectors.counting() method.

The last step returns a map that stores the character and its count.

Java 8 Stream Collectors program:

package com.javaprogramto.w3schools.engineering.string;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Java 8 Stream api collectors example
 *
 * @version JavaProgramTo.com
 */
public class StringCountDuplicateCharJava8Streams {

    public static void main(String[] args) {
        printCountOfDuplicateCharJava8Stream("bbbcccccddddddaaaa");
        printCountOfDuplicateCharJava8Stream("##^$!%^$!^%@!$^@!kds");

    }

    // Using hashmap : print count of each character in a given string.
    private static void printCountOfDuplicateCharJava8Stream(String input) {

        // Step 1
        IntStream intStream = input.chars();

        // Step 2
        Stream<Character> charsStream = intStream.mapToObj(ch -> (char) ch);

        // Step 3
        Map<Character, Long> output = charsStream.collect(Collectors.groupingBy(ch -> ch, Collectors.counting()));

        System.out.println(output);

    }

}

Output:

{b=3, c=5, d=6, a=4}
{#=2, ^=4, $=3, !=4, %=2, @=2, k=1, d=1, s=1}

This program also produced the same output and all 3 steps can be rewritten into a single line as below.

Map<Character, Long> output =  input.chars().mapToObj( ch -> (char) ch ).collect(Collectors.groupingBy(ch -> ch, Collectors.counting()));

6. Working with CodePoints (UTF - Unicode 32-bit values)


Java provides supports for a wide range of characters set within range till 65,535 (0xFFFF). But, later on, lots of other characters introduced and added to the UTF character set. Maximum value has reached 1,114,111 (0x10FFFF).

Typically, We know ASCII characters and those are unprintable control codes between 0-31, printable characters between 32-127, and extended ASCII codes between 128-255.
Java stores the values in 16-bit till 65,535 and all-new characters till 1,114,111 (0x10FFFF) are fall into 32-bit (UTF 32 encoding schema). Java does not support the UTF 32.

You may have a question about what are the new characters added. If your string has characters such as '💕' or '💌' or '💔'. All of these are fall into UTF 32.

What about the remaining characters till 1,114,111 (0x10FFFF).
Java has come up with the solution 'Surrogate Pair' which uses a 2 16-bits approach.

16-bit high surrogates: 1,024 values (U+D800 to U+DBFF)
16-bit low surrogates: 1,024 values (U+DC00 to U+DFFF)

Always a high surrogate followed by a low surrogate and this is known as a surrogate pair. Surrogate pairs are used to represent values between 65,536 (0x10000) and 1,114,111 (0x10FFFF).

Java 8 string api has new methods such as codePointAt(), codePoints(), codePointCount(), and offsetByCodePoints(). We have to use codePointAt() instead of charAt() and codepoints() instead of chars() methods.


String str = String.valueOf(Character.toChars(128140));
System.out.println("128140 : " + str);

str = String.valueOf(Character.toChars(128148));
System.out.println("128148 : " + str);

str = String.valueOf(Character.toChars(128149));
System.out.println("128149 : " + str);


System.out.println("str length : " + str.length());
System.out.println("str codepoints : " + str.codePoints().count());

System.out.println("Code point at index 0 : "+str.codePointAt(0));
System.out.println("Code point at index 1 : "+str.codePointAt(1));

Output:

128140 : 💌
128148 : 💔
128149 : 💕
str length : 2
str codepoints : 1
Code point at index 0 : 128149
Code point at index 1 : 56469

From the above program, codepoints count will be 1 even though string length is 2.

6.1 Surrogate Pair - Java 8 Codepoint Example using compute()


This code problem is implemented by using codepoints().

if the char is surrogate pair then it takes two chars in the input string. But it looks like the only character in the input. So, we need to increment the counter to skip the surrogate pair. For example, the symbol '💕' looks occupied the only character but it is not. It internally takes two chars but considered as one code point.

package com.javaprogramto.w3schools.engineering.string;

import java.util.HashMap;
import java.util.Map;

/**
 * Java 8 Stream api collectors example
 *
 * @version JavaProgramTo.com
 */
public class StringCountDuplicateCharJava8Surrogate {

    public static void main(String[] args) {
        Map<String, Integer> output1 = printCountOfDuplicateCharJava8Surrogate("hello 💌💕  email"); // 💌💕
        System.out.println("output 1 : " + output1);

        Map<String, Integer> output2 = printCountOfDuplicateCharJava8Surrogate("💌💕💌💕💌💕💌💕💌💕💌💕💌💕");
        System.out.println("output 2 : " + output2);
    }

    public static Map<String, Integer> printCountOfDuplicateCharJava8Surrogate(String input) {

        Map<String, Integer> result = new HashMap<>();

        for (int i = 0; i < input.length(); i++) {

            // using codePointAt() in place of charAt() method.
            int cp = input.codePointAt(i);

            // if the char is surrogate pair then it takes two chars in the input string.
            // But it looks like only character. So, we need to increment the counter to skip the surrogate pair
            if (Character.charCount(cp) == 2) { // 2 means a surrogate pair
                i++;
            }

            String ch = String.valueOf(Character.toChars(cp));

            result.compute(ch, (k, v) -> (v == null) ? 1 : ++v);
        }

        return result;
    }

}

Output:

output 1 : { =3, a=1, 💕=1, 💌=1, e=2, h=1, i=1, l=3, m=1, o=1}
output 2 : {💕=7, 💌=7}

6.2 Surrogate Pair - Java 8 Stream API and codepoints()


Let us write the same above code using java 8 functional programming style to cover the Unicode surrogate pairs.


public static Map<String, Integer> printCountOfDuplicateCharJava8Surrogate(String input) {

        Map<String, Long> result = input.codePoints().mapToObj(ch -> String.valueOf(Character.toChars(ch)))
                .collect(Collectors.groupingBy(ch -> ch, Collectors.counting()));

        return result;
    }

Complete for loop can be replaced with the above single line code.

7. Conclusion


In this article, We've seen java programs to count each character how many times repeated in the given string. Covered example code with two for loops which takes lots of time for execution. But, LinkedHashMap usage will be effective in performance-wise.

As well, Java 8 Map API introduced with compute() method which does the containsKey() logic internally and produces the count and functional style code.

At last covered, How to work with Unicode characters if the input contains special characters such as double heart, email symbol. Example programs on Surrogate pairs using java 8 standards.

Note: if you are not sure that input string contents characters if that has Unicode characters and not covered properly then string operations will produce the wrong result. Error proned result is difficult to debug and fix. The recommendation is to use the codepoints API methods as part of the String and Character class.

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java Program To Count Duplicate Characters In String (+Java 8 Program)
Java Program To Count Duplicate Characters In String (+Java 8 Program)
A quick practical and best way to find or count the duplicate characters in a string including special characters. Example programs are shown in various java versions such as java 8, 11, 12 and Surrogate Pairs.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJS0mwLP0VNE0peDN4IwfW1d7s3KXd_53cL7bHBkZb23sdlXJDhkLwBAFjOQRv7WTaun-LB5Ns3eZXCwYel6kKSGLhyphenhyphenoTO5SO0fbVRsYUmLsnIbWK8l0HV7WzHCtRGOJUbkOIH5sdcI5k/s640/Java+Program+To+Count+Duplicate+Characters+In+String+%2528%252BJava+8+Program%2529.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJS0mwLP0VNE0peDN4IwfW1d7s3KXd_53cL7bHBkZb23sdlXJDhkLwBAFjOQRv7WTaun-LB5Ns3eZXCwYel6kKSGLhyphenhyphenoTO5SO0fbVRsYUmLsnIbWK8l0HV7WzHCtRGOJUbkOIH5sdcI5k/s72-c/Java+Program+To+Count+Duplicate+Characters+In+String+%2528%252BJava+8+Program%2529.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/03/java-count-duplicate-characters.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/03/java-count-duplicate-characters.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content