1
int[] array = new int[10];
array[0] = 1;
array[1] = 1;
array[2] = 1;
array[3] = 2;
array[4] = 1;
array[5] = 2;
array[6] = 1;
array[7] = 1;
array[8] = 2;
array[9] = 3;

I want to get this result:

1 => 6 times
2 => 3 times
3 => 1 times

Is there any simple way to get this result without if-else statement?

Thanks.

3
  • I'm not sure where you'd use an if-else statement to get the result at all! :-) Commented Aug 28, 2012 at 13:45
  • If you are asking the independent way of doing this not specific to .NET I would say create a hashmap if the numbers range is small like within 100. I think you got my point. use hashmap. Commented Aug 28, 2012 at 14:06
  • 1
    @wes: I'm not sure how you'd use recursion to do this, either... Commented Aug 28, 2012 at 15:08

6 Answers 6

9

Yes, by using LINQ's GroupBy():

var res = array.GroupBy(x => x)
               .Select(x => new {Number=x.Key,Times=x.Count()})
               .ToList();

Note:
ToList() may not be necessary if you want to iterate over the result just once.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. There Are a lot of answers. First is you. I will accept this. Thanks every body.
I hoped upvoted for my question :) all them are get upvoted but me not :)
@AliRızaAdıyahşi: The reason is that there are plenty of duplicates on SO. No day without GroupBy ;-)
2

You can use LINQ and group the data:

var result = array.GroupBy(x => x)
                  .Select(x => new { Value = x.Key, Count = x.Count() });

Comments

2
array.GroupBy(a => a).Select(a => new { Value = a.Key, Count = a.Count() });

Comments

1

You can use Linq to calculate the results. If you put the following in a Console Application it should work:

using System;
using System.Linq;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[10];
            array[0] = 1;
            array[1] = 1;
            array[2] = 1;
            array[3] = 2;
            array[4] = 1;
            array[5] = 2;
            array[6] = 1;
            array[7] = 1;
            array[8] = 2;
            array[9] = 3;

            var group = from i in array
                        group i by i into g
                        select new
                        {
                            g.Key,
                            Sum = g.Count()
                        };

            foreach (var g in group)
            {
                Console.WriteLine(g.Key + " " + g.Sum);
            }
        }
    }
}

1 Comment

ITYM g.Count() as in the other answers :)
1

This is a Linq statement that will allow you to build your output:

array.GroupBy(x => x).Select(x => new { x.Key, Count = x.Count() } );

Comments

1

Enumerable.GroupBy groups a sequence by a selector. Then you can use the groups to count them or do whatever you need.

var countGroups = array.GroupBy(a => a)
                       .OrderByDescending(g => g.Count());

foreach(var g in countGroups)
    Console.WriteLine("{0} => {1} times", g.Key, g.Count());

Here's a demo with your sample: http://ideone.com/WatNL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.