DEV Community

özkan pakdil
özkan pakdil

Posted on

Counting Duplicates

Count the number of Duplicates

Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
Example

"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (b and B)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice

Solution in java

    public static int duplicateCount(String text) {
        char[] arr = text.toLowerCase().toCharArray();
        Map<Character, Long> result = IntStream
                .range(0, arr.length)
                .mapToObj(i -> arr[i])
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        return Math.toIntExact(result.values().stream().filter(i -> i > 1).count());
    }
Enter fullscreen mode Exit fullscreen mode

Reference: https://www.codewars.com/kata/54bf1c2cd5b56cc47f0007a1/java

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay