## 8 replies

Last post May 22, 2015 03:00 AM by Zhi Lv - MSFT

• MichelleCass

None

0 Points

1 Post

### How can i chek duplicates in C#

<div>How can i check if an array in ascending order and there are three or more occurrence of each distinct value using C#.</div>

For example  int[] Arr=new int[]{1, 1, 1, 3,3,3,3,3,7,7,7,7,7, 8, 8, 8} holds true but int[] Arr2=new int[] { 5,5,8,8,8,9,9,9,9} holds false because there are only two five

array C

• romage

Participant

1230 Points

342 Posts

### Re: How can i chek duplicates in C#

int[] Arr = new int[] { 1, 1,  3, 3, 3, 3, 3, 7, 7, 7, 7, 7, 8, 8, 8 };

Arr.Distinct().All(et => Arr.Count(a => a == et) >= 3);

Hope this helps

Romage

p.s. You could also look at each one individually :

Arr.Distinct().ToList().ForEach(et => Console.WriteLine(Arr.Count(a => a == et) >= 3));

You may prefer just to iterate through a loop, and increment a temp variable.

array C

• ArrayCheck

None

0 Points

2 Posts

### Re: How can i chek duplicates in C#

How can we handle using for loop and going through array count?

array C

• Rion William...

All-Star

113010 Points

18361 Posts

MVP

### Re: How can i chek duplicates in C#

May 20, 2015 12:31 PM|Rion Williams|LINK

If you want to that there are at least three of each distinct element in an array, the easiest approach would to take advantage of the Enumerable.All() method to supply a conditional that would return true or false based on if the condition is met for every element :

```// Define your arrray
var a1 =new int[]{1, 1, 1, 3,3,3,3,3,7,7,7,7,7, 8, 8, 8 };
// Test it (should return true)
var isArrayValid = a1.All(x => a1.Count(y => y == x) >= 3);
```

array C

• Rion William...

All-Star

113010 Points

18361 Posts

MVP

### Re: How can i chek duplicates in C#

May 20, 2015 12:34 PM|Rion Williams|LINK

#### ArrayCheck

How can we handle using for loop and going through array count?

You could simply go through each of the distinct elements and check the count for each :

```public bool DoesArrayHaveAtLeastThreeOfEach(int[] array)
{
// If it is null or empty, ignore it
if(array == null || !array.Any()) { return false; }

// Loop through each value
foreach(var distinctValue in array.Distinct())
{
// Does this element appear less than 3 times?
if(array.Count(x => x == distinctValue) < 3)
{
// Then output false
return false;
}
}

// Otherwise, if it made it here, it's true
return true;
}```

array C

• ArrayCheck

None

0 Points

2 Posts

### Re: How can i chek duplicates in C#

Is that possible to code without the lambda operator? I am not yet familiar with LINQ and the code has to check a[i]<a[i+1]

array C

• Rion William...

All-Star

113010 Points

18361 Posts

MVP

### Re: How can i chek duplicates in C#

May 20, 2015 04:45 PM|Rion Williams|LINK

#### ArrayCheck

Is that possible to code without the lambda operator? I am not yet familiar with LINQ and the code has to check a[i]<a[i+1]

Sure, I suppose since you know that the elements will always be in order, you could use that to loop through :

```public static bool DoesArrayHaveAtLeastThreeOfEach(int[] array)
{
// If it is null or empty, ignore it
if(array == null || array.Length == 0) { return false; }

// Keep track of the current count;
var currentCount = 0;

// Loop through each value
for(int i = 0; i < array.Length; i++)
{
// If it is the first element or the same element, increment the count
if(i == 0 || array[i-1] == array[i])
{
currentCount++;
}
// The values do not match (it's a new element)
else
{
// Check if there were at least 3 of the current element
if(currentCount >= 3)
{
// Reset the count to account for the current new element
currentCount = 1;
}
else
{
// Otherwise, there were less than 2 of this element
return false;
}
}
}

// Otherwise, if it made it here, check the count for the last element
return currentCount >= 3;
}```

array C

• JohnBert

Member

310 Points

111 Posts

### Re: How can i chek duplicates in C#

You need to reference System.Linq (e.g. using System.Linq)

then you can do

```var dupes = dupList.GroupBy(x => new {x.checkThis, x.checkThat})
.Where(x => x.Skip(1).Any());```

This will give you groups with all the duplicates

The test for duplicates would then be

```var hasDupes = dupList.GroupBy(x => new {x.checkThis, x.checkThat})
.Where(x => x.Skip(1).Any()).Any();```

or even call ToList() or ToArray() to force the calculation of the result and then you can both check for dupes and examine them.

eg..

```var dupes = dupList.GroupBy(x => new {x.checkThis, x.checkThat})
.Where(x => x.Skip(1).Any()).ToArray();
if (dupes.Any()) {
foreach (var dupeList in dupes) {
Console.WriteLine(string.Format("checkThis={0},checkThat={1} has {2} duplicates",
duplist.Key.checkThis,
duplist.Key.checkThat,
duplist.Count() - 1));
}

}```

Alternatively

```var dupes = dupList.Select((x, i) => new { index = i, value = x})
.GroupBy(x => new {x.value.checkThis, x.value.checkThat})
.Where(x => x.Skip(1).Any());```

Which give you the groups which each item per group stores the original index in a property index and the item in the property value

array C

• Zhi Lv - MSF...

All-Star

44701 Points

6713 Posts

Microsoft

### Re: How can i chek duplicates in C#

May 22, 2015 03:00 AM|Zhi Lv - MSFT|LINK

Hi MichelleCass,

Welcome to asp.net forum.

As for this issue, you could get the count of the repeat values, then compare the count for symmetry. Please refer to the following code:

```            int[] a1 = new int[] { 1, 1, 1, 3, 3, 3, 3, 3, 7, 7, 7, 7, 7, 8, 8, 8 };

//Get the count of the repeat values.
List<Int32> list = new List<int>();
int count = 1;
for (int i = 0; i < a1.Length - 1; i++)
{
if (a1[i] == a1[i + 1])
{
count++;
}
else
{
count = 1;
}

if (i == a1.Length - 2)
}

//Check the count
bool flag = true;
for (int j = 0; j < list.Count/2; j++)
{
if (list[j] == list[list.Count- 1- j])
{
flag = true;
}
else
{
flag = false;
break;
}
}

if (flag)
Console.Write("true");
else
Console.Write("False");