Count Repeated Strings in a Large List
Arrays & HashingAuto-graded · 20 test casesLast updated
Tests hash-map frequency counting: find every string occurring more than once in a list and report its count, using exact comparison.
You are given a list of strings, which may contain any Unicode characters, including emoji. Find every string that appears more than once and report how many times it appears.
Task
Implement a function that returns a dictionary mapping each repeated string to its number of occurrences in the list.
Rules
- Two strings are the same only if they are exactly equal, code point for code point. Do not apply case folding, whitespace trimming, or Unicode normalization.
"Apple"and"apple"are different strings, and"é"written as one code point differs from"e"followed by a combining accent. - Only strings with a count of at least 2 appear in the result.
- Strings that appear exactly once are left out.
- The empty string
""is an ordinary string and follows the same rules. - If no string repeats, or the list is empty, return an empty dictionary.
- The order of keys in the returned dictionary does not matter.
Constraints
0 <= len(items) <= 10^6- Each string may be empty and may contain arbitrary Unicode characters.
- The total input fits in memory.
- Aim for O(n) expected time, where n is the total size of the input.
Examples
Example 1
Input: repeated_string_counts(['apple', 'banana', 'apple', 'orange', 'banana', 'apple'])
Output: {'apple': 3, 'banana': 2}
Example 2
Input: repeated_string_counts(['hello', 'world', 'hello', '🌍', '🌍'])
Output: {'hello': 2, '🌍': 2}
Hints
Hint 1
A single pass can record how many times each string has been seen. Which data structure gives average O(1) lookups and updates by string?
Hint 2
Count every string first, then keep only the entries whose count is greater than 1. Strings that appear once must not be in the result.
Hint 3
Python strings compare by exact code points, so a dictionary or Counter already treats the strings exactly as the problem requires. No normalization is needed.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Build a frequency table with a hash map, then keep only the entries whose count is greater than 1.
- Walk through the list once and increment a counter for each string. Python's
Counterdoes this directly. - Build the result from the counter, keeping only strings with a count of at least 2.
Why it is correct
After the first pass, the table holds the exact number of occurrences of each distinct string. Python's string equality and hashing are based on exact code points. This matches the required comparison, so no extra normalization is needed. Filtering on count > 1 returns exactly the repeated strings with their true frequencies. An empty list, or a list with no repeats, gives an empty dictionary.
Complexity
- Time: O(T) expected, where T is the total number of characters across all strings. Each string is hashed once, and lookups and updates are O(1) on average beyond hashing.
- Space: O(U), where U is the total size of the distinct strings stored in the table.
Reference solution
from collections import Counter
from typing import Dict, List
def repeated_string_counts(items: List[str]) -> Dict[str, int]:
counts = Counter(items)
return {text: c for text, c in counts.items() if c > 1}