Objective-C algorithm to find largest common subsets of arrays? -
i'm in need of efficient solution finding largest common subsets of multiple arrays.
for example: let's user, chris, wants find other users common interests (from common least common); we'd have compare array of interests other users' arrays , find largest common subset smallest common subset.
chris {bowling, gaming, skating, running}
and other users in database.
brad {bowling, jumping, walking, sitting}
john {bowling, gaming, skating, eating}
sarah {bowling, gaming, drawing, coding}
so chris has common interests, respectively, john, sarah, brad.
how i, in objective-c, able this? pointers great.
you looking algorithm find cardinality of set intersection.
depending on set representation, choose different ways of doing it. performant representation using bits in integer, if number of possible interests exceeds 64 may not easy implement.
a straightforward way of implementing nsmutableset
, this:
// prepare individual lists nsarray *chris = @[@"bowling", @"gaming", @"skating", @"running"]; nsarray *brad = @[@"bowling", @"jumping", @"walking", @"sitting"]; // obtain intersection nsmutableset *common = [nsmutableset setwitarray:chris]; [common intersectset:[nsset setwitharray:brad]]; nslog(@"common interest count: %i", common.count);
Comments
Post a Comment