0

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 number in a horizontal line. Then I compare the same number, then count++, but until know I can’t get the output.

Thanks for helping.

INPUT

46 31 46 9 25 12 45 33 25 12 12 12 28 36 38 28 25 12 12 9 36 38 36 36 12 9 36 12 12 25 28 34 36 36 9 12 16 25 28 44

OUTPUT

9 – 4 12 -10 16 – 1 25 – 5 28 - 4 31 – 1 33 – 1 34 - 1 36 – 7 38 – 2 44 – 1 45 – 1 46 – 2

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
user1262018
  • 1
  • 1
  • 2

5 Answers5

4

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);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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));
}
Phil
  • 42,255
  • 9
  • 100
  • 100
1

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);
}
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • how about if the bundle of number is in combination of 2-itemset?? how to count it. for example.. {1,2} {1,3} {1,4} {2,1} {2,3} {2,4} {3,1} {4,1} ..... itemset {1,4} and {4,1} is assumed same. so, the frequency is 2.. – user1262018 Apr 01 '12 at 15:22
  • so you change your comparison to check item[0] and item[1] – Muad'Dib Apr 01 '12 at 15:30
1

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());
        }
undefined
  • 33,537
  • 22
  • 129
  • 198
  • how about if the bundle of number is in combination of 2-itemset?? how to count it. for example.. {1,2} {1,3} {1,4} {2,1} {2,3} {2,4} {3,1} {4,1} ..... itemset {1,4} and {4,1} is assumed same. so, the frequency is 2.. the output will be like this: {1,2} – 2 {1,3} - 2 {1,4} - 2 {2,3} - 1 {2,4} - 1 help me...T_T tq for your help.. – user1262018 Apr 01 '12 at 15:48
0

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.