Okay, so I've created an array that is defined by the user input, they enter a number to determine the amount of elements, then they put in the value of said elements. My program is supposed to remove duplicates from this array. This is what I have so far, it runs, but not how I want it to. This is what I have so far, however I am missing something or I added something extra. Can someone explain what I'm doing wrong?

` static void Main()

    {

        Console.WriteLine("enter array length ");

        int number = Int32.Parse(Console.ReadLine());


        int[] number1 = new int[number];


        int[] number2 = ElimDuplicates(number1); // array to eliminate duplicates


        foreach (int duplicate in number2)
        {

            Console.Write("{0}, ", duplicate);
        }
        Console.WriteLine();
        Console.ReadLine();
    }
    /// <summary>
    /// Eliminates the Duplicates
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    static int[] ElimDuplicates(int[] number)
    {
        if (number == null || number.Length == 1)
        {
            return number;
        }

        ArrayList list = new ArrayList(number);
        list.Sort();
        for (int i = list.Count - 1; i > 0; i--)
        {
            if ((int)list[i] == (int)list[i - 1]) list.RemoveAt(i);
        }
        return (int[])list.ToArray(typeof(int));
    }


    public static void GetArray(int[] array)
    {

        for (int count = 0; count < array.Length; count++)
        {
            Console.WriteLine("enter a number:");
            array[count] = Convert.ToInt32(Console.ReadLine());
        }
    }
}

}

`

okay, i feel kind of retarded now, but i figured it out. for anyone else that was wondering what i did wrong...i forgot to add a line to go thru for getarray(my bad)...here is the finished code.

` class Program

{

    /// <summary>

    /// Main Program

    /// </summary>

    static void Main()

    {

        Console.WriteLine("enter array length ");

        int number = Int32.Parse(Console.ReadLine());

        int[] number1 = new int[number];
        GetArray(number1);
        int[] number2 = ElimDuplicates(number1); // array to eliminate duplicates

        foreach (int duplicate in number2)
        {

            Console.Write("{0}, ", duplicate);
        }
        Console.WriteLine();
        Console.ReadLine();
    }
    /// <summary>
    /// Eliminates the Duplicates
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    static int[] ElimDuplicates(int[] number)
    {
        if (number == null || number.Length == 1)
        {
            return number;
        }

        ArrayList list = new ArrayList(number);
        list.Sort();
        for (int i = list.Count - 1; i > 0; i--)
        {
            if ((int)list[i] == (int)list[i - 1]) list.RemoveAt(i);
        }
        return (int[])list.ToArray(typeof(int));
    }


    public static void GetArray(int[] array)
    {

        for (int count = 0; count < array.Length; count++)
        {
            Console.WriteLine("enter a number:");
            array[count] = Convert.ToInt32(Console.ReadLine());
        }
    }
}
}

`

Okay, so now I'm thinking about adding a Console.WriteLine that tells the user that it's a duplicate number as soon as it's entered. Any help is always great, I'd love to hear from you, maybe any ideas on what else I could add to this program too.

Hello!
You can define a seprate Function to do so just loop through your whole collection to check if number is there or not e.g.

Static bool Check(ArrayList al,int _numtocheck)
{
bool _isdup=false;
foreach(int item in al)
{
if(al.Contains(_numtocheck))
{
_isdup=true;
}
return _isdup;
}

and than call it like this


Console.WriteLine("Enter any number:");
int num=int.parse(Console.ReadLine());
if(Check(UrArrayList,num))
{
Console.WriteLine("Num is Dup");
}

//i have written code as it is here no copy paste from VS so syntax error may occur and there
// will be 100% good solutions than of mine so you also may check out them :P happy coding

Or you could do it with one line

myIntArray = myIntArray.Distinct().ToArray();

If you want to tell them as they enter, you'd do something like

int position = 0;

while (position < myIntArray.Length) {
    int value;
    String input = Console.ReadLine();
    if (Int32.TryParse(input, out value) {
        if (myIntArray.Contains(value)) {
            Console.WriteLine("You already entered {0}!", value);
        } else {
            myIntArray[position] = value;
            position++;
        }
    } else {
        Console.Writeline("\"{0}\" is not a integer!", input);
    }
}

Note: Zero is not an acceptable value with this code. I'll let you figure out how to enter zero :)

So Momerath, am I to understand most of my coding to find a duplicate can be done in just that one line? If so, that's really awesome.

Okay, I'm moving along here, I've got it...sort of working, but it's throwing me for a loop (hahaha - bad joke). It's not reading my defined element amount right now. I'll post my code when I figure it out, or if I don't and managed to pull my hair out. My relationship with programming is a love/hate thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.