2
\$\begingroup\$

I am currently doing a project for school, and I am completely stumped. We are only on Chapter 8 of C# Development 1 and I have a question about the code.

The project is:

Use a one-dimensional array to solve the following problem:

Write an app that the user inputs numbers, each of which is between 10 and 100, inclusive. As each number is read, display it only if it’s not a duplicate of a number already read. Provide for the “worstcase,” in which all numbers are different. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs each new value.

So far, this is what I have for the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MidtermHandsOn
{
class Program
{
static void Main(string[] args)
{
    //Create array from user input.
    Console.WriteLine("How many numbers will you enter?");
    int Array_Size = Convert.ToInt32(Console.ReadLine());
    int[] Array_Numbers = new int[Array_Size];
    //End array creation.

    //Input numbers into the array. 
    int OuterCounter = 0;
    while (OuterCounter < Array_Size)
    {
        Console.Write("Enter number: ");
        int UserNumber = Convert.ToInt32(Console.ReadLine());

        if (UserNumber >= 10 && UserNumber <= 100) //Check if the numbers are between 10 and 100.
        {
            Array_Numbers[OuterCounter] = UserNumber;

            for (int InnerCounter = 0; InnerCounter < Array_Numbers.Length; InnerCounter++)
            {
                if (Array_Numbers[InnerCounter] > 0)
                { 
                   Console.WriteLine(Array_Numbers[InnerCounter]); 
                }
            }

            OuterCounter++;
        }
        else
        {
            Console.WriteLine("Number must be between 10 and 100");
        }
    }

    Console.ReadKey();
}
}
}

I can get the numbers to input and display in order, which is all well and good, but I cannot for the life of me get the duplicates eliminated.

Now, I'm not wanting the answer to the issue or anything, just wanting to get pointed in the right direction.

Is there ANY way to eliminate duplicates WITHOUT using Linq or Hashset or anything of the sort?

\$\endgroup\$
3
  • \$\begingroup\$ First off you're right that Hashset is what you want in a normal situation. Basically you're trying to create a collection that has an idempotent Add() \$\endgroup\$ – Graeme Bradbury Jul 4 '14 at 22:21
  • 1
    \$\begingroup\$ This question appears to be off-topic because it is about asking for help with the problem solution and not about reviewing the existing code. \$\endgroup\$ – ChrisWue Jul 5 '14 at 1:21
  • \$\begingroup\$ You should only be using Convert.ToInt32 when you know the value is numeric. Your code will blow up if the user enters anything other than a number. \$\endgroup\$ – Mathieu Guindon Jul 5 '14 at 1:58
2
\$\begingroup\$

Not sure whether there's some trickery here... But a simple solution could be:

Simplest way would be an array of boolean for all possible numbers. When you enter 50, check position 50 and if it's false, it was the first time, now set that position to true so it can't be true the next time it is checked. Make it 90 in size and always add 10 to the index :).

bool[] hasBeenEntered = new bool[100-10];

while (true)
{ 
    int nr = Int.Parse(Console.Readline());
    if (nr >= 10 && <= 100)
    {
        if (hasBeenEntered[nr+10])
            continue;

        hasBeenEntered[nr+10] = true;
        Console.WriteLine(nr);
    } 
}

There might be creative solutions using shorts and bit shifting to use a smaller array or actually use all the binary positions in an array of 2 Longs instead to make it a much smaller array. That's probably not the solution direction, but it would work pretty well...

bool[] hasBeenEntered = new long[2];

then check using bitmasks:

if (hasBeenEntered[nr/64] & (nr % 64) > 0)
    continue;

hasBeenEntered[nr/64] |= (nr % 64);
Console.WriteLine(nr);
\$\endgroup\$
2
  • \$\begingroup\$ You don't need to check if it has been entered already. Just set it to true each time. Since multiple assignments of the same value has the same end result as a single assignment. \$\endgroup\$ – Graeme Bradbury Jul 4 '14 at 22:18
  • \$\begingroup\$ One needs to skip the console.writeline though, so a check is needed regardless... \$\endgroup\$ – jessehouwing Jul 5 '14 at 10:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.