数组中相同的值

4

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

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

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

你所说的“check”,是指你会接受一个真/假的答案吗? - Matthew Watson
7个回答

11

使用 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 你能解释一下为什么吗? - I4V
@IV4 您的答案看起来对我来说是正确的... - Matthew Watson
不错的答案,I4V!只是提醒一下, ToList 可以被删除,它仍然可以工作。 - dharmatech

3
抱歉,这个回答是针对问题“如何检查一个数组中是否有两个或多个相同的值?”,但它并没有告诉你实际上有哪些重复项......


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

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 你比我先做到了! - Joachim VR
例如,在这个例子中,我希望程序告诉我有一对2和一对4。 - Andreas Eriksson
@AndreasCarlbom 哦,我明白你的意思了 - 我一开始没有注意到这一点(可能是后来添加的)。当然,我是在回答“如何检查一个数组中是否有两个或更多相等的值?”的问题。 - Matthew Watson

1
您可以使用Linq语句,例如:

您可以使用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() });

0
你可以使用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);
}

0

我喜欢这个语法:

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

0
这是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();
        }
    }
}

-1
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))

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接