
Sponsored
Sponsored
To find duplicate files by their content, we can use a HashMap (or Dictionary). For each directory info string, parse the directory path and the files with their contents. Use the content as the key in the map and store full path of the file as its value. After parsing all inputs, the map keys with more than one value represent duplicate files.
Time Complexity: O(n), where n is the total number of characters in all file paths. We iterate over each character once.
Space Complexity: O(n) to store the lists of file paths in the dictionary.
1function findDuplicate(paths) {
2 const contentMap = {};
3 for (const path of paths) {
4 const [root, ...fileData] = path.split(' ');
5 for (const fileInfo of fileData) {
6 const separatorIndex = fileInfo.indexOf('(');
7 const name = fileInfo.substring(0, separatorIndex);
8 const content = fileInfo.substring(separatorIndex + 1, fileInfo.length - 1);
9 if (!contentMap[content]) {
10 contentMap[content] = [];
11 }
12 contentMap[content].push(`${root}/${name}`);
13 }
14 }
15 return Object.values(contentMap).filter(group => group.length > 1);
16}
17
18// Example Usage:
19const paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)"];
20console.log(findDuplicate(paths));This JavaScript solution uses an object to map file contents to their corresponding file paths. Each path string is split to manage the directory and file details separately. We extract file names and contents from their encoded format and append to the map under the key that matches the file content. Finally, results are filtered for duplicate-identifying groups and returned.
This approach is based on directly processing string data and arranging results using a 2D array. Strings are manipulated to extract directory data, file names, and contents into standalone variables, then append paths to a growing structure. Compared to hash maps, this method uses arrays to aggregate identical files.
Time Complexity: O(n), where n is the total input character count due to one-pass evaluation.
Space Complexity: O(n), maintaining paths and intermediate arrays.
1using System;
2using System.Collections.Generic;
public class DuplicateWithArrays
{
public IList<IList<string>> FindDuplicate(string[] paths)
{
Dictionary<string, List<string>> contentMap = new Dictionary<string, List<string>>();
foreach (string path in paths)
{
string[] parts = path.Split(' ');
string directory = parts[0];
for (int i = 1; i < parts.Length; i++)
{
int openParenIndex = parts[i].IndexOf('(');
int closeParenIndex = parts[i].IndexOf(')', openParenIndex);
string name = parts[i].Substring(0, openParenIndex);
string content = parts[i].Substring(openParenIndex + 1, closeParenIndex - openParenIndex - 1);
if (!contentMap.ContainsKey(content))
{
contentMap[content] = new List<string>();
}
contentMap[content].Add(directory + "/" + name);
}
}
var result = new List<IList<string>>();
foreach (var pair in contentMap)
{
if (pair.Value.Count > 1)
{
result.Add(pair.Value);
}
}
return result;
}
}Utilizing C#'s native string capabilities, this implementation deliberately processes content without relying on heavy structuring, using simple substring and index functions to group matching file paths by their contents.