Search code examples
iosobjective-cnsdictionaryunique

Given a dictionary of numbers, determine which sets contain unique values (i.e. numbers that do not belong to any of the other sets)


Looking for the most efficient way to solve this using Objective-C (and Swift as a second preference).

You have a NSDictionary containing the below sets for example.

NSDictionary dict = @{ 
   { @"foo", [NSNumber numberWithInt:1] },
   { @"bar", [NSNumber numberWithInt:10] },
   { @"baz", [NSNumber numberWithInt:11] },
   { @"boz", [NSNumber numberWithInt:1] }, 
};

I should receive a list containing values (1, 10, 11).


Solution

  • Extract the values (as an array, [dict allValues]) and coerce to an NSSet (initWithArray:).

    You can always coerce back to an array if you really do want an array; but I find in general that often people think they need an array when what they really needed was a set in the first place.