Need to remove duplicate values from C# program

前端 未结 5 1653
南方客
南方客 2020-12-22 07:49

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

5条回答
  •  -上瘾入骨i
    2020-12-22 08:17

    Use Any linq expression to validate duplicates. char.TryParse will validates input and returns true when succeeded.

    public static void Main()
    {
        char[] arr = new char[5];
    
        //User input
        Console.WriteLine("Please Enter 5 Letters only: ");
    
        for (int i = 0; i < arr.Length; i++)
        {
             char input;
    
            if(char.TryParse(Console.ReadLine(), out input) && !arr.Any(c=>c == input))
            {           
    
                arr[i] = input;
            }
            else
            {
                Console.WriteLine( "Error : Either invalid input or a duplicate entry.");
                i--;
            }
        }
    
        Console.WriteLine("You have entered the following inputs: ");
        //display
        for(int i = 0; i

    Working Code

提交回复
热议问题