Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较两个数组并显示字符数_C#_Algorithm_Pseudocode - Fatal编程技术网

C# 比较两个数组并显示字符数

C# 比较两个数组并显示字符数,c#,algorithm,pseudocode,C#,Algorithm,Pseudocode,我有两个阵列: 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 class Program { static void Main(string[] args) { string[] array1 = {"a", "b", "c",

我有两个阵列:

 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
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;
        }
    }
}
我可以在循环中运行一个循环并找到答案,但是有没有更好的方法来实现相同的结果


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

您可以使用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);
var counts=new Dictionary();
foreach(数组1中的字符串项)
{
if(计数容器(项目))
计数[项目]+;
其他的
计数[项目]=1;
}
foreach(数组中的字符串项2)
{
if(计数容器(项目))
计数[项目]+;
其他的
计数[项目]=1;
}
//打印输出计数
foreach(var kvp计数)
WriteLine(“{0}={1}”,kvp.Key,kvp.Value);
请注意,这不会对结果进行排序-如果您需要对结果进行排序,您也必须这样做。

您可以使用,并且:


这看起来像是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));
var charCounts=array1.Concat(array2)
.GroupBy(c=>c)
.Select(g=>newtuple(g.Key,g.Count());
.OrderBy(t=>t.Item1);
foreach(var结果以字符数表示)
WriteLine(String.Format(“{0}={1}”,t.Item1,t.Item2));

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

好的,基于字符串类型的最简单实现如下所示:

a = 3
b = 2
c = 1
d = 1
e = 1
x = 1
y = 1
z = 1
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;
        }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
字符串[]数组1={“a”、“b”、“c”、“d”、“e”};
字符串[]array2={“x”、“y”、“a”、“b”、“a”};
var直方图=新字典();
填充(直方图,数组1);
填充(直方图,数组2);
foreach(直方图中的var p)
{
WriteLine(“{0}={1}”,p.Key,p.Value);
}
}
专用静态空白填充(字典直方图,字符串[]a)
{
foreach(a中的字符串s)
{
若有(直方图中的容器)
直方图[s]+=1;
其他的
直方图[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);
        }
    }
static void Main(字符串[]args)
{
字符串[]数组1={“a”、“b”、“c”、“d”、“e”};
字符串[]array2={“x”、“y”、“a”、“b”、“a”};
字符串[]concat=新字符串[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;

虽然(可能分享一些你拥有的,你可能有最好的解决方案,但如果你不告诉我们你做了什么,我们将不知道!如果你能提供多一点,我将很高兴投票。合并两个数组并进行传统的分组和计数。在这方面有很多Q+a。基于“比较两个数组并显示常用字符的计数”我猜结果应该是[a=3,b=2],因为c,d,e,x,y并不常见。你能澄清一下要求吗?@aiodintsov:谢谢你指出这一点。@Asdfg但答案是什么?它是否会包括c,d,e,x,y?如果它包括->它的concat(array1,array2)->直方图,如果不是,那么它就是ar3=intersect(array1,array2)->(concat(array1,array2)对ar3的直方图。除此之外,还有什么要求?c2,c3,c4?禁止LINQ?禁止数组方法?等等?目标框架2.0,3.0,3.5,4.0,4.5?除了使用字符外,你还必须使用字符串吗?那么你的数组可以是{“aaa”,“word2”,“else”}?或者它们确实是字符数组{'a','b','C'}仅?我不想使用LINQ。我只想知道使用普通C#可以有多高的效率。没有指定的@Asdfg-但是有理由避免使用LINQ吗?@Asdfg-LINQ是“仅使用普通C#”。它是语言的一部分;不是扩展或外部库/工具。@Asdfg是一种完全不同的语言-具有完全不同的库和规则集。尽管如此,我向您展示了另一种使用循环和类的方法,而不是使用任何函数代码。+1用于适应提供LINQ的附加需求和非LINQ解决方案。