Javascript 创建组而不重复以前的组

Javascript 创建组而不重复以前的组,javascript,arrays,algorithm,heuristics,Javascript,Arrays,Algorithm,Heuristics,我创建了一个random群组创建者,但random并不能保证你能与以前从未合作过的人一起工作。如果有人能够生成一个“具有历史记录的随机群组生成器”来跟踪以前的群组,并避免将用户反复放入具有相同用户的群组,我肯定会使用它!有人知道怎么做吗 为清楚起见:给定一个字符串数组 ["Jason", "Kim", "Callie", "Luke"] 和一个先前配对的数组(也是数组) 返回重复组成员数最少的分组 [["Jason", "Callie"], ["Luke", "Kim"]] 我在想象,我想尽

我创建了一个random群组创建者,但random并不能保证你能与以前从未合作过的人一起工作。如果有人能够生成一个“具有历史记录的随机群组生成器”来跟踪以前的群组,并避免将用户反复放入具有相同用户的群组,我肯定会使用它!有人知道怎么做吗

为清楚起见:给定一个字符串数组

["Jason", "Kim", "Callie", "Luke"]
和一个先前配对的数组(也是数组)

返回重复组成员数最少的分组

[["Jason", "Callie"], ["Luke", "Kim"]]
我在想象,我想尽量减少的是重复伴侣的数量。因此,对于每一对两个人来说,每次他们已经加入一个团队,如果结果将他们放在同一个团队中,那么结果就会得到这个分数。例如,获得返回值的“评分”可能如下所示:

["Jason", "Kim"] have a score of 1, they have been paired together before
["Callie", "Luke"] have a score of 1, they have been paired together before
["Jason", "Luke"] have a score of 1, they have been paired together before
["Callie", "Kim"] have a score of 1, they have been paired together before
["Jason", "Callie"] have a score of 0, they have not been paired together before
["Luke", "Kim"] have a score of 0, they have not been paired together before
选择覆盖整个列表的集合,同时生成最小分数。在这种情况下,配对[“Jason”、“Callie”]和[“Luke”、“Kim”]覆盖整个集合,并且得分为0(无重复分组),因此这是一个最佳解决方案(0是可能的最佳结果)

这可能是一种错误的方法(因为我想象它需要n平方时间),但希望它能让我明白我在努力优化什么。这不需要是一个完美的优化,只是一个“体面的答案”,不会每次都把相同的组放在一起


理想情况下,它能够处理任何大小的组,也能够处理当天可能有人外出的事实(并非所有人都会在所有阵列中)。我想要一个javascript答案,但如果有人能提出逻辑,我应该能够翻译。

您可以收集对象中的所有配对并计数。然后只取计数较小的

函数getKey(数组){ 返回数组.slice().sort().join(“|”); } var strings=[“Jason”、“Kim”、“Callie”、“Luke”], 数据=[[“杰森”、“金姆”]、[“卡莉”、“卢克”]、[[“杰森”、“卢克”]、[“卡莉”、“金姆”]], 对象={}, i、 j, 钥匙; 对于(i=0;i
。作为控制台包装{max height:100%!important;top:0;}
请添加一些想法如何从第二行到第三行得到结果。您可以查找加权随机数sampling@NinaScholz我希望这有帮助?我试图对这些步骤进行更深入的解释。这是一种获取计数的超级聪明的方法。这当然是我想要开始的地方,但是我如何“只取计数较小的”。有没有一种简单的方法可以取计数较小的,并且仍然保证完全覆盖(每个人都在一个组中)?
["Jason", "Kim"] have a score of 1, they have been paired together before
["Callie", "Luke"] have a score of 1, they have been paired together before
["Jason", "Luke"] have a score of 1, they have been paired together before
["Callie", "Kim"] have a score of 1, they have been paired together before
["Jason", "Callie"] have a score of 0, they have not been paired together before
["Luke", "Kim"] have a score of 0, they have not been paired together before