Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program to remove duplicates in int array

Tags:

c#

I have written some code to remove duplicates from Integer Array. I dont want to use any inbuilt keywords/property.

Here is my logic :

int[] iArray = {1,2,3,2,3,4,3};              
int t = 0;
int arraysize = iArray.Length;
for (int m = 0; m < arraysize; m++)
{
    if (iArray[m] != iArray[t])
    {
        t++;
        iArray[t] = iArray[m];
    }
}
arraysize = t + 1;
for (int m = 0; m < arraysize; m++)
{
    Console.WriteLine(iArray[m]);
}

Output should be:

1,2,3,4

It does not give the desired output. Guys, this is not the homewok. This is Self Learning. No LINQ,Contains keyword please. Thank you for you replies.

Thanks.

like image 922
Bokambo Avatar asked Aug 22 '12 12:08

Bokambo


People also ask

How do you remove the duplicate number on a given integer array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.


1 Answers

Since this is a homework, I would not fix your code, and give you a few notes instead:

  1. You would not be able to do it with a single loop and no built-in functions; you need two nested loops
  2. When you find a duplicate value, move the array items forward by one, and decrease the size of the "active area" of the array; you need another nested loop to do the move.
like image 167
Sergey Kalinichenko Avatar answered Sep 22 '22 22:09

Sergey Kalinichenko