Group Identical Files by Content from an In-Memory Listing
Arrays & HashingAuto-graded · 14 test casesLast updated
Group file entries that have identical byte content, using content length as a cheap first filter, and return only groups of two or more paths in a specified order.
Find Files With Identical Content
You are given a snapshot of a file collection as a list of entries. Each entry is a dictionary with two keys:
"path": a string, the file's path."content": a list of integers in the range0..255, the file's bytes in order.
Write a function that groups together the paths of files whose contents are exactly equal, and returns only the groups that contain at least two paths.
Rules
- Two files are duplicates if and only if their
contentlists are identical (same length, same values in the same order). - Each group in the result is a list of paths. A file whose content is unique must not appear anywhere in the result, so every group has at least 2 paths.
- Empty files (
content == []) are duplicates of each other. If two or more exist, they form one group. - Files of different lengths can never be duplicates, so a sensible solution compares lengths first and only compares content among files of equal length.
Output order
The result must be deterministic:
- Paths inside a group appear in the same relative order as in the input.
- Groups are ordered first by when their content length was first seen in the input (a length seen earlier comes first). Groups with the same length are ordered by the first appearance of their content in the input.
If no duplicates exist, return an empty list.
Constraints
0 <= len(files) <= 10^4- Each
pathis a non-empty string; all paths are distinct. - Each
contenthas length between0and10^4, with every value in0..255. - The total number of bytes across all files is at most about
10^6.
Examples
Example 1
Input: group_identical_files([{'path': '/data/a.txt', 'content': [104, 101, 108, 108, 111]}, {'path': '/data/b.txt', 'content': [104, 101, 108, 108, 111]}, {'path': '/data/c.txt', 'content': [119, 111, 114, 108, 100]}, {'path': '/data/d.txt', 'content': [120]}, {'path': '/data/sub/e.txt', 'content': [104, 101, 108, 108, 111]}])
Output: [['/data/a.txt', '/data/b.txt', '/data/sub/e.txt']]
Example 2
Input: group_identical_files([{'path': '/data/empty-a', 'content': []}, {'path': '/data/empty-b', 'content': []}])
Output: [['/data/empty-a', '/data/empty-b']]
Hints
Hint 1
Two files with different lengths can never match, so start by bucketing entries by the length of their content.
Hint 2
Inside a bucket, convert each content list to something hashable (for example `bytes` or a tuple) and use it as a dictionary key that maps to a list of paths.
Hint 3
Python dictionaries keep insertion order. Using them at both levels gives you the required output order for free. Finally, keep only the groups with at least two paths.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Duplicates must have the same length, so we filter in two stages, cheapest first.
- Bucket by length. Walk the entries once and append each
(path, content)pair to a dictionary keyed bylen(content). Any length that appears only once cannot contain a duplicate, so that bucket is skipped without comparing any content. This mirrors the "don't read file data unless sizes collide" idea. - Group by content inside each bucket. For every bucket with two or more files, convert each content list to
bytes(hashable and compact) and use it as a dictionary key that maps to the list of paths. Keep the groups with at least two paths.
Empty files all land in the length-0 bucket and share the key b"", so they form one group naturally when there are two or more.
Why it is correct
- Equal content implies equal length, so stage 1 never separates true duplicates.
- Within a bucket, the dictionary key is the exact content, so two files share a group if and only if their contents are identical. There are no hash collisions, because the key is the content itself.
- Python dictionaries preserve insertion order. Buckets are therefore visited in order of first-seen length, groups inside a bucket appear in order of first-seen content, and the paths in each group are in input order. This matches the required output order.
Complexity
Let N be the number of files and B the total number of bytes.
- Time: O(N + B). Each file is bucketed in O(1) and converted and hashed once, and only if its length collides with another file's.
- Space: O(N + B) for the buckets and the byte keys.
Reference solution
from typing import List, Dict
def group_identical_files(files: List[Dict]) -> List[List[str]]:
# Stage 1: bucket by content length (dict preserves first-seen order).
by_length = {}
for entry in files:
content = entry["content"]
by_length.setdefault(len(content), []).append((entry["path"], content))
result = []
for length, items in by_length.items():
if len(items) < 2:
continue
# Stage 2: within equal-length files, group by exact content.
by_content = {}
for path, content in items:
by_content.setdefault(bytes(content), []).append(path)
for paths in by_content.values():
if len(paths) >= 2:
result.append(paths)
return result