Write a method that returns an array of all the Duplicate Files

java logo
java logo

Coding Challenge: Duplicate Files, Write Code to Find that out.

Explanation: You left your computer unlocked and your friend decided to troll you by copying a lot of your files to random spots all over your file system. Even worse, she saved the duplicate files with random, embarrassing names.

So Your Task is to Write a method that returns an array of all the duplicate files. 

We’ll check them by hand before actually deleting them, since programmatically deleting files is really scary. So you help us to confirm that two files are actually duplicates and return an array of arrays where:

  • the first item is the duplicate file
  • the second item is the original file

For example:

[

  [“/tmp/parker_is_dumb.mpg”, “/home/parker/secret_puppy_dance.mpg”],

  [ “/home/trololol.mov”, “/etc/apache2/httpd.conf”]

]

You can assume each file was only duplicated once.

Notes:

  • Are you correctly handling child folders as well as sibling folders? Be careful that you’re traversing your file tree correctly.
  • When you find two files that are the same, don’t just choose a random one to mark as the “duplicate”. Try to figure out which one your friend made!
  • Does your solution work correctly if it’s an empty file system (meaning the root directory is empty)?
  • Is your solution order of the total size on disc of all the files? If so, you can do better!

To Solve this problem we will look into string mapping and if we find the exact match we will return both files.

Here is working Code Example:

public class Solution {
    public List < List < String >> findDuplicate(String[] paths) {
        HashMap < String, List < String >> map = new HashMap < > ();
        for (String path: paths) {
            String[] values = path.split(" ");
            for (int i = 1; i < values.length; i++) {
                String[] name_cont = values[i].split("\\(");
                name_cont[1] = name_cont[1].replace(")", "");
                List < String > list = map.getOrDefault(name_cont[1], new ArrayList < String > ());
                list.add(values[0] + "/" + name_cont[0]);
                map.put(name_cont[1], list);
            }
        }
        List < List < String >> res = new ArrayList < > ();
        for (String key: map.keySet()) {
            if (map.get(key).size() > 1)
                res.add(map.get(key));
        }
        return res;
    }
}