Objective-C algorithm for finding the largest common subsets of arrays?

Currently, I need an effective solution to find the most common subsets of several arrays.

For example: Say a user, Chris, wants to find other users with common interests (from the most common to the least common); we would have to compare his array of interests with arrays of other users and find the largest common subset for the smallest common subset.

Chris (bowling, games, skating, running)

And other users in the database.

Brad {bowling, jumping, walking, sitting}
John {bowling, game, skating, riding}
Sara {bowling, game, drawing, coding}

So, Chris has the most common interests, respectively, with John, then with Sarah, then with Brad.

How could I do this in Objective-C? Any pointers would be great.

+4
source share
1 answer

You are looking for an algorithm to find the power of multiple intersections.

Depending on your presentation of the set, you can choose different ways to execute it. The most indicative of this would be the use of bits in an integer, but if the number of possible interests exceeds 64, it can be difficult to implement.

An easy way to implement it would be an NSMutableSet , for example:

 // Prepare the individual lists NSArray *chris = @[@"bowling", @"gaming", @"skating", @"running"]; NSArray *brad = @[@"bowling", @"jumping", @"walking", @"sitting"]; // Obtain the intersection NSMutableSet *common = [NSMutableSet setWitArray:chris]; [common intersectSet:[NSSet setWithArray:brad]]; NSLog(@"Common interest count: %i", common.count); 
+5
source

Source: https://habr.com/ru/post/1499519/


All Articles