¿Encontrar valores de matriz con conteo?

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;

Quiero obtener este resultado:

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

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

Gracias.

preguntado el 28 de agosto de 12 a las 14:08

I'm not sure where you'd use an if-else statement to get the result at all! :-) -

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. -

@wes: I'm not sure how you'd use recursion to do this, either... -

6 Respuestas

Yes, by using LINQ's Agrupar por():

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

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

Respondido 28 ago 12, 14:08

Thanks. There Are a lot of answers. First is you. I will accept this. Thanks every body. - AliRıza Adıyahşi

I hoped upvoted for my question :) all them are get upvoted but me not :) - AliRıza Adıyahşi

@AliRızaAdıyahşi: The reason is that there are plenty of duplicates on SO. No day without GroupBy ;-)- Tim Schmelter

You can use LINQ and group the data:

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

Respondido 28 ago 12, 14:08

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

Respondido 28 ago 12, 14:08

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

Respondido 28 ago 12, 14:08

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

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

Respondido 28 ago 12, 14:08

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

Respondido 28 ago 12, 14:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.