需要从C#程序中删除重复值 [英] Need to remove duplicate values from C# program

查看:93
本文介绍了需要从C#程序中删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助与我创建一个C#程序。因此,在这种情况下我正在输入重复的值到程序中。例如,A,B,B,C,C。结果
演习的是,如果有任何输入字母重复(无数字)我应该得到一个错误消息:重复的值,请重试!并且将不接受重复的值,并应显示的数值为A,B,C,D,E。

 类节目
{
静态无效的主要(字串[] args)
{
的char [] =改编新的char [5];

//用户输入
Console.WriteLine(请输入只有5个字母:);

的for(int i = 0; I< arr.Length;我++)
{
改编[I] = Convert.ToChar(到Console.ReadLine());
}
//显示
的for(int i = 0; I< arr.Length;我++)
{
Console.WriteLine(你输入以下输入:);
Console.WriteLine(arrArray [I]);
}
}
}


解决方案

选择合适的数据结构的开始,来代替阵列HashSet的,因为业务主要寻找向上和插入。


I need some help with a C# program that i am creating. So in this scenario i am inputting duplicate values into the program. For Example, a,b,b,c,c.
The exercise is that if there are any duplicated letters inputted (no numbers) i should get an error stating "Duplicate Value. Please Try Again!" and will not accept the duplicate value, and should show the values as a,b,c,d,e.

class Program
{
    static void Main(string[] args)
    {
    char[] arr = new char[5];

    //User input
    Console.WriteLine("Please Enter 5 Letters only: ");

    for (int i = 0; i < arr.Length; i++)
    {
        arr[i] = Convert.ToChar(Console.ReadLine());
    }
    //display
    for(int i = 0; i<arr.Length; i++)
    {
        Console.WriteLine("You have entered the following inputs: ");
        Console.WriteLine(arrArray[i]);
    }
 }
}

解决方案

Choose right data structure at beginning, use HashSet instead of array since the operations are mainly looking up & inserting.

这篇关于需要从C#程序中删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆