Skip to content
CodeKruPro

Find the duplicate characters and their counts

Coding problemEasy-MediumAsked in: Accenture, Capgemini

Given a string s, find the characters that occur more than once, and how many times each occurs.

Return one entry per repeated character, in the order the characters first appear in s. Write each entry as the character, an equals sign and the count, such as "r=2".

Skip spaces. Upper and lower case are different characters. If no character repeats, return an empty list.

Examples

Example 1
Input: s = "programming"
Output: ["r=2","g=2","m=2"]
Example 2
Input: s = "abc"
Output: []
Example 3
Input: s = "a a A"
Output: ["a=2"]
Spaces are skipped, and A is a different character from a.

Constraints

  • The length of s is between 0 and 100,001
  • s holds printable ASCII characters only

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in