Skip to main content
Back to problems
#609
Medium Algorithms

Find duplicate file in system

Array Hash Table String
67.6% acceptance
Feb 20, 2026
1559
1657
Given a list paths of directory info, return all the duplicate files in the file system in terms of their paths.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
use std::collections::HashMap;
impl Solution {
  pub fn find_duplicate(paths: Vec<String>) -> Vec<Vec<String>> {
    let mut map: HashMap<String, Vec<String>> = HashMap::new();
    for path in &paths {
      let parts: Vec<&str> = path.split_whitespace().collect();
      let dir = parts[0];
      for file in &parts[1..] {
        if let Some(paren) = file.find('(') {
          let name = &file[..paren];
          let content = &file[paren + 1..file.len() - 1];
          let full_path = format!("{}/{}", dir, name);
          map.entry(content.to_string()).or_default().push(full_path);
        }
      }
    }
    map.into_values().filter(|v| v.len() > 1).collect()
  }
}