Momo
Momo

Reputation: 523

How to display how many time an array value is in the array?

I have an array which contains duplicate values. I need to display how many times each value is found in an array.

Let us say I have an array of 8 values, array = {1,2,3,1,1,2,6,7} I need the output to be:

1 was found 3 times
2 was found 2 times
3 was found 1 time
6 was found 1 time
7 was found 1 time

Here is my code. For now I am saving each value in the array to a variable and then loop through the array to check if the value is there then print it out.

 int[] nums = { 2, 4, 14, 17, 45, 48, 5, 6, 16, 25, 28, 33, 17, 26, 35, 44, 46, 49, 5, 6, 20, 27, 36, 45, 6, 22, 23, 24, 33, 39, 4, 6, 11, 14, 15, 38, 5, 20, 22, 26, 29, 47, 7, 14, 16, 24, 31, 32 };
            for (int i = 0; i < nums.Length; i++)
            {
                int s = nums[i];
                for (int j = 0; j < nums.Length; j++)
                {
                    if (s == nums[j])
                    {
                        Console.WriteLine(s);
                    }
                }

            }

Thanks in advance

Upvotes: 3

Views: 188

Answers (4)

bash.d
bash.d

Reputation: 13207

Are you working with arrays for pure education? C# offers Collections which provide a lot of convenient functionality to address such problems. The System.Collections.Dictionary offers the functionality you are looking for. Adding an item, if it doesn't exist and react, when a key has already been added.

using System.Collections.Generic;

Dictionary<int,int> dic = new Dictionary<int, int>();
if(!dic.Keys.Contains(key))
   //add key and value
else 
  //get key and add value

See MSDN for this.

Upvotes: 0

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98740

How about using .Key and .Count after Grouping and Ordering them?

foreach(var g in nums.GroupBy(x => x).OrderBy(g => g.Key))
{
    Console.WriteLine("{0} was found {1} times", g.Key, g.Count());
}

Here is a DEMO.

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062550

foreach(var grp in nums.GroupBy(x => x).OrderBy(grp => grp.Key)) {
    Console.WriteLine("{0} was found {1} times", grp.Key, grp.Count());
}

The GroupBy puts all the values into groups using the number itself as the key (via x => x). For each unique value we'll have a different group with one or more values in it. The OrderBy ensures we report the groups in key-order (via grp => grp.Key). Finally, the Count tells us how many items are in the group identified by Key (the original value, if you recall).

Upvotes: 13

user1193035
user1193035

Reputation:

You can handle this via Enumerable.GroupBy. I recommend looking at the C# LINQ samples section on Count and GroupBy for guidance.

In your case, this can be:

int[] values = new []{2, 4, 14, 17, 45, 48, 5, 6, 16, 25, 28, 33, 17, 26, 35, 44, 46, 49, 5, 6, 20, 27, 36, 45, 6, 22, 23, 24, 33, 39, 4, 6, 11, 14, 15, 38, 5, 20, 22, 26, 29, 47, 7, 14, 16, 24, 31, 32};

var groups = values.GroupBy(v => v);
foreach(var group in groups)
    Console.WriteLine("{0} was found {1} times", group.Key, group.Count());

Upvotes: 0

Related Questions