Need to remove duplicate values from C# program

前端 未结 5 1651
南方客
南方客 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条回答
  • 2020-12-22 08:03

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

    0 讨论(0)
  • 2020-12-22 08:03

    Keep it simple, and although a HashSet is nice semantically, it's not needed for 5 elements (it's actually slower than a List in that case). Worse, it requires a parallel structure to track the characters (assuming you care about order).

    Clearly none of these considerations matter for such a small example but it's good to learn them up front and don't always jump to big-O notation when actually measured performance and memory consumption should be your guide for most practical applications.

    Instead you can simply do:-

    List<char> chars = new List<char>(5);
    while (chars.Count < 5)
    {
       char c = Console.ReadKey().KeyChar;
       if (!char.IsLetter(c)) continue;
       if (chars.Contains(char)) continue;
       chars.Add(char);
    }
    

    Plus whatever error messages you want to add.

    0 讨论(0)
  • 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<arr.Length; i++)
        {
    
            Console.WriteLine(arr[i]);
        }
    
    }
    

    Working Code

    0 讨论(0)
  • 2020-12-22 08:19

    Using a hashtable (Generic Dictionary) is an efficient way to determine if an entered character has already been encountered.

    Also, the Char.IsLetter method in the .NET framework is a great way to check for bad data.

    static void Main(string[] args) {
        Dictionary<char, bool> charsEntered = new Dictionary<char, bool>();
    
        Console.WriteLine("Please enter 5 characters, each on a separate line.");
        while (charsEntered.Count() < 5) {
            Console.WriteLine("Enter a character:");
            char[] resultChars = Console.ReadLine().ToCharArray();
    
            if(resultChars.Length != 1 || !Char.IsLetter(resultChars[0])) {
                Console.WriteLine("Bad Entry. Try again.");
            } else {
                char charEntered = resultChars[0];
                if (charsEntered.ContainsKey(charEntered))
                    Console.WriteLine("Character already encountered. Try again.");
                else
                    charsEntered[charEntered] = true;
            }
        }
    
        Console.WriteLine("The following inputs were entered:");
        Console.WriteLine(String.Join(", ", charsEntered.Keys));
        Console.ReadLine();
    }
    
    0 讨论(0)
  • 2020-12-22 08:25

    Elaborating on Shelvin's answer of using HashSet

    HashSet<char> chars = new HashSet<char>();
    
    //User input
    Console.WriteLine("Please Enter 5 Letters only: ");
    for (int i = 0; i < 5; )
    {
         char c = Convert.ToChar(Console.ReadLine());
         if(!("abcdefghijklmnopqrstuvwxyz".Contains(c.ToString().ToLower())))
         {
              Console.WriteLine("Please enter an alphabet");
              continue;
         }
         else if (!chars.Contains(c))
         {
              chars.Add(c);
              i++;
         }
         else
         {
              Console.WriteLine("Duplicate value please try again");
              continue;
         }
    }
    //display
    Console.WriteLine("You have entered the following inputs: ");
    foreach(char c in chars)
        Console.WriteLine(c.ToString());
    
    Console.Read();
    
    0 讨论(0)
提交回复
热议问题