4

How can I check if there are two or more equal values in one array?

eg. in this example, i want the program to tell me that there is a pair of 2 and a pair of 4

int[] array1 = { 1, 2, 4, 2, 4 };
  • 1
    possible duplicate of [How to Count Duplicates in List with LINQ](http://stackoverflow.com/questions/454601/how-to-count-duplicates-in-list-with-linq) or [C# list array duplicates with count](http://stackoverflow.com/questions/7832602/c-sharp-list-array-duplicates-with-count). – Tim Schmelter May 27 '13 at 09:23
  • By "check" do you mean you will accept a true/false answer? – Matthew Watson May 27 '13 at 09:24

7 Answers7

11

Using 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);
}
I4V
  • 34,891
  • 6
  • 67
  • 79
3

[EDIT] Sorry, this answers the question "How can I check if there are two or more equal values in one array?", but it doesn't tell you the actual duplicates...


This would work, but possibly it isn't the most efficient way!

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

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

If you want the most efficient approach:

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

I don't think you can get much more efficient than that. It will return as soon as it finds any match.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
1

You could use a Linq Statement like:

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

This will return the following:

Item 2: ItemCount 2
Item 4: ItemCount 2

Or another syntax for the same:

var query = array1.GroupBy(x => x)
                  .Where(x => x.Count() > 1)
                  .Select(x => new { x, Count = x.Count() });
jAC
  • 5,195
  • 6
  • 40
  • 55
0

You could use LINQ's GroupBy

Example:

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);
}
Andreas Eriksson
  • 8,979
  • 8
  • 47
  • 65
0

I like this syntax:

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);
Kaveh Shahbazian
  • 13,088
  • 13
  • 80
  • 139
0

Here's a slight variation of I4V's answer.

Instead of Select and ToList this uses ToDictionary.

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();
        }
    }
}
dharmatech
  • 8,979
  • 8
  • 42
  • 88
-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))

}
Ria
  • 10,237
  • 3
  • 33
  • 60
gasroot
  • 515
  • 3
  • 15