简体   繁体   中英

How to find the most Frequent (n-elements) Combinations in list(array) of arrays

I'm trying to find out a way to list the most frequent combinations in a list of arrays. It has to be at least two elements that appears more than once in one of the arrays. Then showing up the most frequents by numeric order. Suppose I have a list of arrays and I would like to know the most common combinations.

List=:

Array (4) ["a", "b", "c", "d"]
Array (4) ["b", "c", "d"]
Array (4) ["c", "d"]
Array (4) ["a", "b"]
Array (4) ["a", "c", "d"]

Desired Result:

No of occur - Elements
4 - ["c","d"]
2 - ["b","c"]; ["a","b"]; ["a","c","d"]

How can I get most commons combinations of elements in all of those arrays?

In this case it is ["c","d"], because they occur 4 times.

Most common combinations of two elements or more in an array.

Thanks.

If the number of unique elements you expect is small enough, you can use this approach:

  • Determine all possible combinations using the unique elements in your data set. In this case abcd , abc , abd , bcd , ab , etc.
  • For every combination, check how many of the input arrays contain all of the combination's characters

The number of possible combinations will rapidly increase as you have more unique elements in your data (eg all characters from a to z).

If that's the case but the individual lists are still small, I'd suggest to not look at all unique combinations, but use the combinations that actually appear in the data.

 //// Utils // Get all unique sets of a size const allSets = (xs, size) => size === 1? xs: xs.flatMap( (x, i) => allSets(xs.slice(i + 1), size - 1).map(tail => [x, ...tail])) // Get a range of ints between two values const range = (from, to) => Array.from({ length: to - from + 1 }, (_, i) => from + i); // Get all unique values from an array const uniques = xs => Array.from(new Set(xs)); //// App const input = [ ["a", "b", "c", "d"], ["b", "c", "d"], ["c", "d"], ["a", "b"], ["a", "c", "d"]]; const uniqueChars = uniques(input.flat()); const combinations = range(2, 4).flatMap(size => allSets(uniqueChars, size)); // Create sets so we can use.has instead of.includes const inputSets = input.map(xs => new Set(xs)); // For all combinations, check how many sets include all of the characters const combinationScores = combinations.map(combo => [ // Check how many sets contain all the chars of this combo inputSets.filter(s => combo.every(x => s.has(x))).length, // Join the set for easy logging combo.join("") ]) // Sort by appearance count.sort( ([c1], [c2]) => c2 - c1); // Print to console console.log(combinationScores.join("\n"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM