# Duplication Elimination Project [closed]

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;

namespace MidtermHandsOn
{
class Program
{
static void Main(string[] args)
{
//Create array from user input.
Console.WriteLine("How many numbers will you enter?");
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: ");

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");
}
}

}
}
}


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?

• 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() – Graeme Bradbury Jul 4 '14 at 22:21
• 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. – ChrisWue Jul 5 '14 at 1:21
• 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. – Mathieu Guindon Jul 5 '14 at 1:58

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)
{
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];


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