How to count the frequency of bundle of number using c#?

前端 未结 5 1147
遇见更好的自我
遇见更好的自我 2020-12-12 06:21

Can somebody help me to run this program using c#. This program is to calculate the frequency of the number, for example 12 appear 10x. Before this I try to sort all list

相关标签:
5条回答
  • 2020-12-12 07:00

    Assuming you have a list of numbers:

    var numbers = new List<int>{ 1, 2, 3, 4, 1, 2, 2, 3 };
    

    Then we can use Linq to achieve what you want:

    var frequencies = 
        numbers.GroupBy( n => n ).Select( n => new { Value=n.Key, Count=n.Count() } );
    
    foreach (var f in frequencies)
    {
        Debug.WriteLine(string.Format("Value={0}, Frequency={1}", f.Value, f.Count));
    }
    
    0 讨论(0)
  • 2020-12-12 07:06

    Homework assignment? We're not here to do homework assignments for you, but I will give you advice on what to do.

    First, I would have a text reader so your input can be inputted. Then, parse each entry and add it into a dictionary. Then, iterate thru the dictionary to see how many times a specific entry occurs.

    0 讨论(0)
  • 2020-12-12 07:10

    I would use a dictionary of int and int: Dictionary and iterate through the numbers adding 1 as you go. some solutions use an array, but I prefer a dictionary this eliminates the need to manage the size of the array and is more memory efficient.

    int[] someValues = { /* your numbers */ }
    Dictionary<int,int> Counts = new Dictionary<int,int>();
    foreach(int key in someValues)
    {
        if ( !Counts.HasKey(key) ) Counts[ key ] = 0;
        Counts[key] = Counts[key] + 1;
    }
    

    then, you just iterate over the dictionary for your output:

    foreach(KeyValuePair<int,int> kvp in Counts)
    {
        Console.Write("{0} - {1}",kvp.Key,kvp.Value);
    }
    
    0 讨论(0)
  • 2020-12-12 07:20

    Well, you could do this manually using a Dictionary<int, int>:

    var frequencies = new Dictionary<int, int>();
    foreach (var item in data)
    {
        int currentCount;
        // We don't care about the return value here, as if it's false that'll
        // leave currentCount as 0, which is what we want
        frequencies.TryGetValue(item, out currentCount);
        frequencies[item] = currentCount + 1;
    }
    

    A simpler but less efficient approach would be to use LINQ:

    var frequencies = data.ToLookup(x => x) // Or use GroupBy. Equivalent here...
                          .Select(x => new { Value = x.Key, Count = x.Count() })
                          .ToList();
    foreach (var frequency in frequencies)
    {
        Console.WriteLine("{0} - {1}", frequency.Value, frequency.Count);
    }
    
    0 讨论(0)
  • 2020-12-12 07:23

    I would put them all into a list then use a group by clause to group them ie

            List<int> numbers = new List<int> { 1, 2, 2, 3, 5, 6, 2, 1, 4, 4 };
            foreach (var group in numbers.GroupBy(n => n))
            {
                Console.WriteLine("{0} was found {1} times", group.Key, group.Count());
            }
    
    0 讨论(0)
提交回复
热议问题