Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am facing issues with eliminating duplicates in my array. I want the user to enter 5 unique numbers. Each time a duplicate entry is entered, I want a "Duplicate entry. Try again." message to appear until the user enters a valid number.

When the program runs:
if you enter 1 then 2 then 2 again, the "Duplicate entry" message pops up. But then when you enter a new value like 3, it still pops up.

In the final display of the elements in the array: each element contains the last number that you entered.

What I have tried:

public void numberCheck()
       {
           int[] numbers = new int[5];

           Console.WriteLine("Enter 5 Numbers: ");

           var count = 0;
           while (count < 5)
           {
               bool duplicate = false;
               var input = Convert.ToInt32(Console.ReadLine());

               for (var i = 0; i < numbers.Length; i++)
               {
                   numbers[i] = input;
                   var check = numbers[i];
                   for (var j = i + 1; j < numbers.Length; j++)
                   {
                       if (numbers[i] == numbers[j])
                       {
                           duplicate = true;
                       }
                       while (duplicate == true)
                       {
                           Console.WriteLine("Duplicate entry. Try again.");
                           input = Convert.ToInt32(Console.ReadLine());

                           if (input != check)
                           {
                               //numbers[i] = input;
                               duplicate = false;
                           }
                       }
                   }
               }
               count++;
           }

           Console.WriteLine();

           // print out numbers array
           foreach (var n in numbers)
           {
               Console.WriteLine(n);
           }
       }
   }
Posted
Updated 4-Dec-17 3:40am

Cant you just use a list instead?
C#
List<int> myList = new List<int>();

if (!myList.Conatins(item))
         myList.Add(item);
 
Share this answer
 
Comments
SCloudQ 4-Dec-17 9:38am    
Thank you very much for your solution!
Use an HashSet<int>[^] instead of an array.
 
Share this answer
 
Comments
SCloudQ 4-Dec-17 9:38am    
Thank you very much for your solution!
CPallini 4-Dec-17 12:39pm    
You are welcome.
Your error is in these lines:
C#
for (var i = 0; i < numbers.Length; i++)
{
    numbers[i] = input;
    // ...
    count++;
}
You are initially assigning the input to all array elements overwriting all values entered previously.

Think about the requirement. It is not difficult (based on your code):
C#
while (count < 5)
{
    var input = Convert.ToInt32(Console.ReadLine());
    bool duplicate = CheckDuplicate(input, numbers, count);
    if (duplicate)
    {
        Console.WriteLine("Duplicate entry. Try again.");
    }
    else
    {
        numbers[count] = input;
        count++;
    }
}
All you have to do is writing a function that checks for a duplicate (or replace the function call by code doing the same). The check requires three variables: the input value, the array, and the number of values added so far.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900