So it looks like you trying to get map of MD5 hash to list of files with that hash. It may be better to directly express that in data strucutre:
var hashToFiles = new Dictionary<string, List<string>>();
Now when processing new file you have hash
+ fileName
- so you can check if it is already in the map and add new/update existing entry:
if (hashToFiles.ContainsKey(hash))
{
// add new entry
hashToFiles.Add(hash, new List<string>{fileName});
}
else
{
hashToFiles[hash].Add(fileName);
}
So with map build the only thing left is to find items with more than one elements
var keyValueForDups = hashToFiles.Where(item => item.Value.Length > 1);
Notes:
- using SHA256 is better than outdated MD5, but ok for your purposes
- in your current code you are recreating array every time instead of using list and appending to it
- use custom class to hold
{hash, fileName}
pair to make code readable