繁体   English   中英

如何在 arrays 的列表(数组)中找到最频繁的(n 个元素)组合

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

我试图找到一种方法来列出 arrays 列表中最常见的组合。 它必须至少有两个元素在 arrays 之一中出现不止一次。 然后按数字顺序显示最频繁的。 假设我有一个 arrays 列表,我想知道最常见的组合。

列表=:

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

期望的结果:

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

如何在所有这些 arrays 中获得最常见的元素组合?

在这种情况下,它是 ["c","d"],因为它们出现了 4 次。

数组中两个或多个元素的最常见组合。

谢谢。

如果您期望的唯一元素数量足够少,您可以使用以下方法:

  • 使用数据集中的独特元素确定所有可能的组合。 在这种情况下abcdabcabdbcdab等。
  • 对于每个组合,检查有多少输入 arrays 包含所有组合的字符

随着数据中有更多独特元素(例如从 a 到 z 的所有字符),可能的组合数量将迅速增加。

如果是这种情况,但单个列表仍然很小,我建议不要查看所有唯一组合,而是使用实际出现在数据中的组合。

 //// 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"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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