0

I have a dictionary of strings, where some keys have duplicate values to other keys like below.

var dict: [String: String] =
    ["key1": "value1",
    "key2": "value1",
    "key3": "value2",
    "key4": "value1"]

How do I get the count of the duplicates of the values (e.g. count of value1 = 3, count of value2 = 1)? I have seen some info on how to do this with booleans, but I have not figured out how to do it with strings. Thanks.

Arkyyn
  • 51
  • 1
  • 10
  • 2
    `NSCountedSet` on `dict.values` should do the trick. Else `dict.values` to Array (`Array(dict.values)` ?), then https://stackoverflow.com/questions/46996966/swift-4-how-to-return-a-count-of-duplicate-values-from-an-array – Larme Feb 26 '21 at 15:31
  • Thanks. That nudge helped a lot. I had been playing around with the solutions in the post you referenced all morning, but had not stumbled on using `dict.values`. Thanks again. – Arkyyn Feb 26 '21 at 16:18

0 Answers0