<bdo id='cvuod'></bdo><ul id='cvuod'></ul>

    <tfoot id='cvuod'></tfoot>

    1. <i id='cvuod'><tr id='cvuod'><dt id='cvuod'><q id='cvuod'><span id='cvuod'><b id='cvuod'><form id='cvuod'><ins id='cvuod'></ins><ul id='cvuod'></ul><sub id='cvuod'></sub></form><legend id='cvuod'></legend><bdo id='cvuod'><pre id='cvuod'><center id='cvuod'></center></pre></bdo></b><th id='cvuod'></th></span></q></dt></tr></i><div id='cvuod'><tfoot id='cvuod'></tfoot><dl id='cvuod'><fieldset id='cvuod'></fieldset></dl></div>
      <legend id='cvuod'><style id='cvuod'><dir id='cvuod'><q id='cvuod'></q></dir></style></legend>
    2. <small id='cvuod'></small><noframes id='cvuod'>

      如何在保留重复项的同时进行整数列表交集?

      时间:2023-08-28
      <tfoot id='Y8dTP'></tfoot>

        <tbody id='Y8dTP'></tbody>

        <bdo id='Y8dTP'></bdo><ul id='Y8dTP'></ul>
        1. <small id='Y8dTP'></small><noframes id='Y8dTP'>

              <legend id='Y8dTP'><style id='Y8dTP'><dir id='Y8dTP'><q id='Y8dTP'></q></dir></style></legend>
              <i id='Y8dTP'><tr id='Y8dTP'><dt id='Y8dTP'><q id='Y8dTP'><span id='Y8dTP'><b id='Y8dTP'><form id='Y8dTP'><ins id='Y8dTP'></ins><ul id='Y8dTP'></ul><sub id='Y8dTP'></sub></form><legend id='Y8dTP'></legend><bdo id='Y8dTP'><pre id='Y8dTP'><center id='Y8dTP'></center></pre></bdo></b><th id='Y8dTP'></th></span></q></dt></tr></i><div id='Y8dTP'><tfoot id='Y8dTP'></tfoot><dl id='Y8dTP'><fieldset id='Y8dTP'></fieldset></dl></div>
              • 本文介绍了如何在保留重复项的同时进行整数列表交集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在处理最大公因数和最小公因数分配,我必须列出公因数.Intersection() 将不起作用,因为它会删除重复项.Contains() 将不起作用,因为如果它在第二个列表中看到 int,它会从第一个列表中返回所有匹配的 int.有没有办法做一个不相干的交叉点?

                I'm working on a Greatest Common Factor and Least Common Multiple assignment and I have to list the common factors. Intersection() won't work because that removes duplicates. Contains() won't work because if it sees the int in the second list it returns all matching ints from the first list. Is there a way to do an Intersection that is not Distinct?

                抱歉没有提供示例,这就是我的意思:

                edit: sorry for not providing an example, here is what I meant:

                如果我有套装:

                {1, 2, 2, 2, 3, 3, 4, 5}
                {1, 1, 2, 2, 3, 3, 3, 4, 4}
                

                我想要输出

                {1, 2, 2, 3, 3, 4}
                

                推荐答案

                ILookup<int, int> lookup1 = list1.ToLookup(i => i);
                ILookup<int, int> lookup2 = list2.ToLookup(i => i);
                
                int[] result =
                (
                  from group1 in lookup1
                  let group2 = lookup2[group1.Key]
                  where group2.Any()
                  let smallerGroup = group1.Count() < group2.Count() ? group1 : group2
                  from i in smallerGroup
                  select i
                ).ToArray();
                

                where 表达式在技术上是可选的,我觉得它使意图更清晰.

                The where expression is technically optional, I feel it makes the intent clearer.

                如果你想要更简洁的代码:

                If you want more terse code:

                ILookup<int, int> lookup2 = list2.ToLookup(i => i);
                
                int[] result =
                (
                  from group1 in list1.GroupBy(i => i)
                  let group2 = lookup2[group1.Key]
                  from i in (group1.Count() < group2.Count() ? group1 : group2)
                  select i
                ).ToArray();
                

                这篇关于如何在保留重复项的同时进行整数列表交集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:您如何获得 ToDictionary() 失败的重复键? 下一篇:使用 PhysicalAddress 作为键时字典中的重复键

                相关文章

                最新文章

                <tfoot id='TL1hG'></tfoot>
                1. <i id='TL1hG'><tr id='TL1hG'><dt id='TL1hG'><q id='TL1hG'><span id='TL1hG'><b id='TL1hG'><form id='TL1hG'><ins id='TL1hG'></ins><ul id='TL1hG'></ul><sub id='TL1hG'></sub></form><legend id='TL1hG'></legend><bdo id='TL1hG'><pre id='TL1hG'><center id='TL1hG'></center></pre></bdo></b><th id='TL1hG'></th></span></q></dt></tr></i><div id='TL1hG'><tfoot id='TL1hG'></tfoot><dl id='TL1hG'><fieldset id='TL1hG'></fieldset></dl></div>
                    <legend id='TL1hG'><style id='TL1hG'><dir id='TL1hG'><q id='TL1hG'></q></dir></style></legend>

                    <small id='TL1hG'></small><noframes id='TL1hG'>

                      <bdo id='TL1hG'></bdo><ul id='TL1hG'></ul>