0

Error Index was outside the bounds of the array. Program is to remove duplicates from an array I understand the error which is that line 32 where an extra element is created at the end of the for loop but i am unable to figure out a way to display the whole array without the duplicates. What the prog is suppose to do is inputs a 5 number into an array and then sorts them and if their are duplicates it removes it. I hope i made it clear!

     using System;

     class duplicate 
     {
        static void Main() 
        {
            const int Array_Size = 5;
            int [] number = new int [Array_Size];
            int i;

    for ( i = 0; i < Array_Size; i++) 
    {
        number[i] = Int32.Parse(Console.ReadLine());
        if (number[i] < 9 || number[i] > 101)
        {
            Console.WriteLine("Enter Number between 10 - 100");
            number[i] = Int32.Parse(Console.ReadLine());
        }
    }

    Array.Sort(number);

    Console.WriteLine("Sorted Array : ");

    for (i = 0; i < Array_Size; i++)
    {
        Console.WriteLine("Element is " + number[i]);
    }

    Console.WriteLine("Duplicate Removed : ");

    for (i = 0; i < Array_Size; i++)
    { 
        if (number[i] != number[i+1])
            Console.WriteLine("Element is " + number[i]);
    }

    Console.ReadLine();
}

}

4 Answers 4

2

The exception is happening at this line:

if (number[i] != number[i+1])

On the final iteration of your loop, that line will resolve to:

if(number[4] != number[5])

Since your array only has 5 items in it and is 0-based, number[5] results in your IndexOutOfRange exception.

If you're trying to compare every item to the one after it, simply don't check on the final iteration:

if(i != Array_Size && number[i] != number[i+1])

Or only loop up to Array_Size - 1:

for (i = 0; i < Array_Size - 1; i++)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use LINQ to get distinct values from an Array:

var distinct = number.Distinct().ToArray();

You need using System.Linq; on top of your file to make it work.

And to make it clear: your Exception comes from here:

for (i = 0; i < Array_Size; i++)
{ 
    if (number[i] != number[i+1])
        Console.WriteLine("Element is " + number[i]);
}

When i == Array_Size -1 (which is the last pass) number[i+1] does not exist.

Comments

0

Of course Linq is the way to go today, but you could still use the old fashioned loop

int prev = number[0];
Console.WriteLine("Element is " + prev);
for (int i = 1; i < Array_Size; i++)
{ 
    int cur = number[i];
    if (cur != prev)
    {
        Console.WriteLine("Element is " + cur);
        prev = cur;
    }
}

Comments

0

This if statement is simply unsafe

for (i = 0; i < Array_Size; i++) {
   if (number[i] != number[i+1])
     ...

The final value of i in the loop will be Array_Size - 1 hence i + 1 isn't a legal index into the array. To fix this just change the constraint on the for loop such that i + 1 is always a legal index

for (i = 0; i < Array_Size - 1; i++)

1 Comment

If i use for (i = 0; i < Array_Size - 1; i++) It doesn't display the last element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.