C# find repeated numbers in an array
Integer Array has numbers ,some are repeated, if you want to find which number is repeated how many time., Here is the logic using LINQ.
static void FindRepeatedNumbers()
{
int[] a=new int[]{10,20,100,100,100,10,30,20,40,50,12,14};
var query = from d in a
group d by d into da
select da;
int maximum = a.Max();
foreach (IGrouping<int, int> intagroup in query)
{
Console.WriteLine("Key={0},Repeated {1} Times",intagroup.Key,intagroup.Count());
}
{
int[] a=new int[]{10,20,100,100,100,10,30,20,40,50,12,14};
var query = from d in a
group d by d into da
select da;
int maximum = a.Max();
foreach (IGrouping<int, int> intagroup in query)
{
Console.WriteLine("Key={0},Repeated {1} Times",intagroup.Key,intagroup.Count());
}
}
- First group by each number
- there will a unique groups ,then find each group has how many numbers.
Number=10,Repeated 2 Times
Number=20,Repeated 2 Times
Number=100,Repeated 3 Times
Number=30,Repeated 1 Times
Number=40,Repeated 1 Times
Number=50,Repeated 1 Times
Number=12,Repeated 1 Times
Number=14,Repeated 1 Times
Tags: C# Find repeated numbers in an array,Find duplicate numbers in an array, find maximum repeated number in an array,find least repeated number in an array
No comments:
Post a Comment