-2

我有两个数组:

 string[] array1 = {"a","b","c","d","e"}

 string[] array1 = {"x","y","a","b","a"}

我想打印这样的结果:

a = 3
b = 2
c = 1
d = 1
e = 1
x = 1
y = 1
z = 1

我可以在循环内运行一个循环并找出它,但有没有更好的方法来实现相同的结果?

我想在不使用 LINQ 的普通 C# 中执行此操作。

4

5 回答 5

8

您可以使用 LINQ 来完成此操作:

var counts = array1.Concat(array2)
                  .GroupBy(v => v)
                  .Select(g => new { Value=g.Key, Number=g.Count() });

foreach(var item in counts.OrderBy(i => i.Value))
    Console.WriteLine("{0} = {1}", item.Value, item.Number);

鉴于出于某种原因您想避免使用 LINQ 和扩展方法,您可以构建自己的字典:

var counts = new Dictionary<string, int>();
foreach(string item in array1)
{
    if (counts.ContainsKey(item))
        counts[item]++;
    else
        counts[item] = 1;
}
foreach(string item in array2)
{
    if (counts.ContainsKey(item))
        counts[item]++;
    else
        counts[item] = 1;
}

// Print out counts
foreach(var kvp in counts)
    Console.WriteLine("{0} = {1}", kvp.Key, kvp.Value);

请注意,这不会对结果进行排序 - 如果您需要对它们进行排序,您也必须这样做。

于 2012-09-05T20:38:22.477 回答
5

您可以使用Concat,GroupByOrderByDescending:

var both = array1.Concat(array2);
var groups = both.GroupBy(s => s).OrderByDescending(g => g.Count());
Console.Write(
    String.Join(
        Environment.NewLine, 
            groups.Select(g => String.Format("{0} = {1}", g.Key, g.Count()))));
于 2012-09-05T20:38:34.593 回答
1

这看起来像是 Linq 的工作:

var charCounts = array1.Concat(array2)
                    .GroupBy(c=>c)
                    .Select(g=>new Tuple<char, int>(g.Key, g.Count());
                    .OrderBy(t=>t.Item1);

foreach(var result in charCounts)
   Console.WriteLine(String.Format("{0} = {1}", t.Item1, t.Item2));
于 2012-09-05T20:39:09.487 回答
1

通读这两个数组并将它们放入一个字典中。键是数组中的成员,如“a”、“b”等。值是整数作为计数。因此,如果存在键,则增加计数;否则将键放入字典中,值为 1。

于 2012-09-05T20:44:46.053 回答
0

好吧,基于字符串类型的最简单的实现是这样的:

class Program
{
    static void Main(string[] args)
    {
        string[] array1 = {"a", "b", "c", "d", "e"};
        string[] array2 = {"x", "y", "a", "b", "a"};

        var histogram = new Dictionary<string, int>();
        Fill(histogram, array1);
        Fill(histogram, array2);

        foreach (var p in histogram)
        {
            Console.WriteLine("{0}={1}",p.Key,p.Value);
        }
    }

    private static void Fill(Dictionary<string, int> histogram, string[] a)
    {
        foreach (string s in a)
        {
            if (histogram.ContainsKey(s))
                histogram[s] += 1;
            else
                histogram[s] = 1;
        }
    }
}

这是构建动态直方图,打印。另一种简单的方法是这样的,但它的可读性较差:

    static void Main(string[] args)
    {
        string[] array1 = {"a", "b", "c", "d", "e"};
        string[] array2 = {"x", "y", "a", "b", "a"};

        string [] concat = new string[array1.Length+array2.Length];
        Array.Copy(array1,concat,array1.Length);
        Array.Copy(array2,0,concat,array1.Length,array2.Length);
        Array.Sort(concat);

        int pos = 0;
        while(pos<concat.Length)
        {
            var cur = concat[pos];
            int count = 0;
            while ( (pos<concat.Length) && (concat[pos]==cur))
            {
                pos += 1;
                count += 1;
            }
            Console.WriteLine("{0}={1}",cur,count);
        }
    }

通常 - 排序数据的连接,排序,直方图。

于 2012-09-05T21:17:37.553 回答