swift - Most common dictionary in array of dictionaries -


i need find common dictionary in array of swift dictionaries. tried using following :

func frequencies                 <s: sequencetype s.generator.element: hashable>                 (source: s) -> [(s.generator.element,int)] {                      var frequency: [s.generator.element:int] = [:]                      x in source {                         frequency[x] = (frequency[x] ?? 0) + 1                     }                      return sorted(frequency) { $0.1 > $1.1 }             } 

but can't invoke 'frequencies' argument list of type [[string:string]](). how can edit above function take array of dictionaries, or use method entirely?

you use nscountedset provides kind of functionality:

let arr = [     [         "foo": "bar"     ],     [         "foo": "bar"     ],     [         "foo": "baz"     ], ]  let countedset = nscountedset(array: arr) dict in countedset {     println(countedset.countforobject(dict))     println(dict) } 

prints

2

["foo": "bar"]

1

["foo": "baz"]

if you're having problems initializer not woking properly, use:

let countedset = nscountedset() countedset.addobjectsfromarray(arr) 

instead of

let countedset = nscountedset(array: arr) 

i haven't tested swift 2, usage should more or less same.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -