Created
June 18, 2021 12:40
-
-
Save harrydayexe/9591759fd17a49a59535c74350431153 to your computer and use it in GitHub Desktop.
A function to remove duplicate values from a dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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