【问题标题】:Equal values in array [duplicate]数组中的相等值[重复]
【发布时间】:2013-05-28 13:22:01
【问题描述】:

如何检查一个数组中是否有两个或多个相等的值?

例如。在这个例子中,我想让程序告诉我有一对 2 和一对 4

int[] array1 = { 1, 2, 4, 2, 4 };

【问题讨论】:

标签: c# arrays


【解决方案1】:

使用 Linq

var result = array1.GroupBy(i=>i)
                .Select(g=>new {Value = g.Key, Count = g.Count()})
                .Where(x=>x.Count>1)
                .ToList();

foreach (var pair in result)
{
     Console.WriteLine("PAIR: " + pair.Value + " COUNT: " + pair.Count);
}

【讨论】:

  • @SerialDownvoter 你能解释一下原因吗?
  • @IV4 你的回答对我来说似乎是正确的......
  • 不错的答案 I4V!请注意,ToList 可以删除,它仍然可以工作。
【解决方案2】:

[编辑] 抱歉,这回答了问题“我如何检查一个数组中是否有两个或多个相等的值?”,但它并没有告诉你实际的重复.. .


这可行,但可能不是最有效的方法!

int[] array1 = { 1, 2, 4, 2, 4 };

if (array1.Distinct().Count() < array1.Length)
    Console.WriteLine("Contains Dupes");

如果您想要最有效的方法:

bool containsDupes(int[] array)
{
    for (int i = 0; i < array.Length - 1; ++i)
    {
        int n = array[i];

        for (int j = i+1; j < array.Length; ++j)
            if (array[j] == n)
                return true;
    }

    return false;
}

我认为没有比这更有效率的了。它会在找到任何匹配项后立即返回。

【讨论】:

  • +1 你打败了我!
  • 例如。在这个例子中,我希望程序告诉我有一对 2 和一对 4
  • @AndreasCarlbom 啊,我明白你的意思了——一开始我没有注意到(可能是后来添加的)。我在回答“如何检查一个数组中是否有两个或多个相等的值?”当然。
【解决方案3】:

您可以使用如下 Linq 语句:

            var query =
                from numbers in array1
                group numbers by numbers into duplicates
                where duplicates.Count() > 1
                select new { Item = duplicates.Key, ItemCount = duplicates.Count() };

这将返回以下内容:

Item 2: ItemCount 2
Item 4: ItemCount 2

或相同的另一种语法:

var query = array1.GroupBy(x => x)
                  .Where(x => x.Count() > 1)
                  .Select(x => new { x, Count = x.Count() });

【讨论】:

    【解决方案4】:

    你可以使用 LINQ 的 GroupBy

    例子:

    var grouped = array1.GroupBy(x => x).Select(x => new { Value = x.Key, Count = x.Count() });
    
    foreach(var item in grouped) {
        if (item.Count == 1)
            continue;
    
        Console.WriteLine("There are {0} instances of the number {1} in the array.", item.Count, item.Value);
    }
    

    【讨论】:

      【解决方案5】:

      我喜欢这种语法:

      int[] array1 = { 1, 2, 4, 2, 4 };
      
      var isThereAnyRepeated = (from i in array1
                                  group i by i into g
                                  where g.Count() > 1
                                  select g).Any();
      
      Console.WriteLine(isThereAnyRepeated);
      

      【讨论】:

        【解决方案6】:

        这是 I4V 的答案略有不同。

        这里使用ToDictionary,而不是SelectToList

        using System;
        using System.Linq;
        
        namespace StackOverflow_2013_05_27_EqualValuesInArray
        {
            class Program
            {
                static void Main(string[] args)
                {
                    int[] array = { 1, 2, 4, 2, 4 };
        
                    var tbl = array
                        .GroupBy(n => n)
                        .Where(g => g.Count() > 1)
                        .ToDictionary(g => g.Key, g => g.Count());
        
                    foreach (var pair in tbl)
                        Console.WriteLine("{0} is in array {1} times", pair.Key, pair.Value);
        
                    Console.ReadLine();
                }
            }
        }
        

        【讨论】:

          【解决方案7】:
          class item
          {
              int value;
              int number;
          }
          list<item> items = new list <item>();
          
          
          for(int i=0; i<array1.length;i++)
          {
              if (i=0)
              items.add(new item(array1[i],1))
          
              else if (array1.contains(array[i])) items.add(new item(array1[i],))
              else items.add(new item(array1[i],1))
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-29
            • 1970-01-01
            • 2019-03-04
            • 2019-09-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多