LeetCode: Find Duplicate File In System Solution

1/**
2 * @param {string[]} paths
3 * @return {string[][]}
4 */
5var findDuplicate = function (paths) {
6 const contentPathMap = new Map()
7
8 paths.forEach(rawPath => {
9 let [dirPath, ...files] = rawPath.split(" ")
10
11 // extract content
12 files = files.map(file =>
13 file.replace("(", " ").replace(")", "").split(" ")
14 )
15
16 files.forEach(([fileName, content]) => {
17 const filePath = `${dirPath}/${fileName}`
18 if (contentPathMap.has(content)) {
19 contentPathMap.get(content).push(filePath)
20 } else {
21 contentPathMap.set(content, [filePath])
22 }
23 })
24 })
25
26 return [...contentPathMap.values()].filter(filePaths => filePaths.length > 1)
27}

Comments

Loading comments...

Tags

leetcode

hash table

string

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Binary Tree Level Order Traversal

May 20, 2021

Previous Post

HoningJS

Search Posts