有兴趣的朋友看看,一个简单的算法?计算出一个随即数组每个字符串出现的个数

时间:2023-01-09 11:26:53
计算出一个随即数组每个字符串出现的个数。

string[] str = {"aa","bb","cc","dd","aa","ff","ff","aa","cc","dd","ee","gg","cc","bb"};

以上数组里出现的字符串是不固定的。

用一种简单高效率的算法求出结果
结果
aa:3个  bb:2个 cc:3个 dd:2个 ff:2个 ee:1个 gg:1个
 

5 个解决方案

#1


        Dictionary<string, int> dic = new Dictionary<string, int>();
        foreach (string key in str)
        {
            if (dic.ContainsKey(key))
            {
                dic[key]++;
            }
            else
            {
                dic.Add(key, 1);
            }
        }
        System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
        foreach (string key in dic.Keys)
        {
            sb.AppendFormat("{0}:{1}个   ",key,dic[key]);
        }

#2


引用 1 楼 flashcjy 的回复:
        Dictionary <string, int> dic = new Dictionary <string, int>();
        foreach (string key in str)
        {
            if (dic.ContainsKey(key))
            {
                dic[key]++;
            }
            else
            {
                dic.Add(key, 1);
            }
        }
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (string key in dic.Keys)
        {
            sb.AppendFormat("{0}:{1}个  ",key,dic[key]);
        }

#3


这种问题用Linq最简单...
var groups = str.GroupBy(s => s).ToDictionary(d => d.Key, d => d.Count());
foreach (var item in groups)
{
    Console.WriteLine("{0}:{1}", item.Key, item.Value);
}

#4


1楼的比较正规,我给个没技术含量的linq!

using System;
using System.Linq;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strs = { "aa", "bb", "cc", "dd", "aa", "ff", "ff", "aa", "cc", "dd", "ee", "gg", "cc", "bb" };
            var g = from item in strs group item by item into groups select groups;

            foreach (var item in g)
                Console.Write("{0}:{1}个 ", item.Key, item.Count());
        }
    }
}

#5



学习学习。谢谢!

#1


        Dictionary<string, int> dic = new Dictionary<string, int>();
        foreach (string key in str)
        {
            if (dic.ContainsKey(key))
            {
                dic[key]++;
            }
            else
            {
                dic.Add(key, 1);
            }
        }
        System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
        foreach (string key in dic.Keys)
        {
            sb.AppendFormat("{0}:{1}个   ",key,dic[key]);
        }

#2


引用 1 楼 flashcjy 的回复:
        Dictionary <string, int> dic = new Dictionary <string, int>();
        foreach (string key in str)
        {
            if (dic.ContainsKey(key))
            {
                dic[key]++;
            }
            else
            {
                dic.Add(key, 1);
            }
        }
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (string key in dic.Keys)
        {
            sb.AppendFormat("{0}:{1}个  ",key,dic[key]);
        }

#3


这种问题用Linq最简单...
var groups = str.GroupBy(s => s).ToDictionary(d => d.Key, d => d.Count());
foreach (var item in groups)
{
    Console.WriteLine("{0}:{1}", item.Key, item.Value);
}

#4


1楼的比较正规,我给个没技术含量的linq!

using System;
using System.Linq;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strs = { "aa", "bb", "cc", "dd", "aa", "ff", "ff", "aa", "cc", "dd", "ee", "gg", "cc", "bb" };
            var g = from item in strs group item by item into groups select groups;

            foreach (var item in g)
                Console.Write("{0}:{1}个 ", item.Key, item.Count());
        }
    }
}

#5



学习学习。谢谢!