简体   繁体   English

从字典 c# 中删除多余的值

[英]remove excess values from dictionary c#

I have the following program to detect duplicate values and print distinct values from an integer array.我有以下程序来检测重复值并从整数数组中打印不同的值。 but the Dictionary value prints one extra line value 0 occurred 95 times <- How to remove this line?但是字典值打印了一个额外的行value 0 occurred 95 times <- 如何删除这一行?

class CheckRepeat
{
    public static void Main()
    {
        int[] a = new int[100];

        int n,i, j;
        Console.Write("\n\nCount total number of duplicate elements in an array:\n");
        Console.Write("---------------------------------------------------------\n");

        Console.Write("Input the number of elements to be stored in the array :");
        n = Convert.ToInt32(Console.ReadLine());

        /*---------------------------------------Input Array-------------------------------------*/
        Console.Write("Input {0} elements in the array :\n", n);
        for (i = 0; i < n; i++)
        {
            Console.Write("element - {0} : ", i);
            a[i] = Convert.ToInt32(Console.ReadLine());
        }
        var dict = new Dictionary<int, int>();
        /*------------------------------------Store Duplicates-----------------------------------*/
        foreach (var value in a)
        {
            if (dict.ContainsKey(value))
                dict[value]++;
            else
                dict[value] = 1;
        }
        /*------------------------------------Show Duplicates-----------------------------------*/
        foreach (var pair in dict)
            Console.WriteLine("Value {0} occurred {1} times.", pair.Key, pair.Value);

        Console.WriteLine("Distinct Values");
        int[] q = a.Distinct().ToArray();
        for (i = 0; i < q.Length-1; i++) 
        {
            Console.WriteLine(q[i]);
        }

        Console.Read();
    }
}

You have two corrections.你有两个更正。

First, your Array is declared with Size 100. This would prefill your array with 0s.首先,您的数组声明为大小 100。这将用 0 预填充您的数组。 This is the reason you have the extra element (0) and its count.这就是您拥有额外元素 (0) 及其计数的原因。 You could instead create the Array with same size as the desired number of elements.您可以改为创建与所需元素数量相同大小的数组。

Console.Write("Input the number of elements to be stored in the array :");
n = Convert.ToInt32(Console.ReadLine());
int[] a = new int[n]; // Change here

Secondly, you need correct loop condition where you print the Distinct Element其次,您需要正确的循环条件来打印不同的元素

use

i < q.Length;

Instead of代替

 i < q.Length-1;

Complete Loop完整循环

for (i = 0; i < q.Length; i++) 
{
   Console.WriteLine(q[i]);
}

Alternate Solution替代解决方案

You could also use List<T> and Linq to make this more simpler.您还可以使用List<T>Linq使这更简单。 For example,例如,

    Console.Write("Input the number of elements to be stored in the array :");
    n = Convert.ToInt32(Console.ReadLine());
    List<int> a = new List<int>();

    for (i = 0; i < n; i++)
    {
            Console.Write("element - {0} : ", i);
            a.Add(Convert.ToInt32(Console.ReadLine()));
    }

    var dict = a.GroupBy(x=>x).ToDictionary(x=>x.Key,y=>y.Count());

    Console.WriteLine("Duplicates");
    foreach(var pair in dict)
    {
        Console.WriteLine($"{pair.Key} occured {pair.Value} times");
    }

    Console.WriteLine("Distinct");
    foreach(var pair in dict)
    {
        Console.WriteLine($"{pair.Key}");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM