Skip to content

Instantly share code, notes, and snippets.

@harrydayexe
Created June 18, 2021 12:40
Show Gist options
  • Save harrydayexe/9591759fd17a49a59535c74350431153 to your computer and use it in GitHub Desktop.
Save harrydayexe/9591759fd17a49a59535c74350431153 to your computer and use it in GitHub Desktop.
A function to remove duplicate values from a dictionary
/**
This function removes duplicates from any dictionary
- Parameter dict: The input dictionary to remove duplicate values from
- Returns: The same dictionary with duplicates removed
*/
private static func removeDuplicates<Key: Hashable, Value: Hashable>(fromDict dict: Dictionary<Key, Value>) -> Dictionary<Key, Value> {
var uniqueValues = Set<Value>()
var resultDict = [Key : Value](minimumCapacity: dict.count)
for (key, value) in dict {
if !uniqueValues.contains(value) {
uniqueValues.insert(value)
resultDict[key] = value
}
}
return resultDict
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment