# Thread: Removing duplicate value in array

1. ## Removing duplicate value in array

I am trying to remove duplicates value in array

sequence is [1, 2, 1, 2, 5]

Program should give output [ 1, 2, 0, 0, 5]

1 == 2
1 == 1 found replace with zero 1 2 0 2 5
1 == 2
1 == 5

2== 1
2 == 2 found replace with zero 1 2 0 0 5
1 == 2
1 == 5
....... and son

I wrote code but struggling with inner loop to update array value

Code:
```#include<stdio.h>

int main()
{

int array[5] = { 1, 2, 1, 2, 5};

for ( int i = 0; i < 5; i ++)
{
for ( int j = 0; j < 5; j++)
{
if ( array[i] == array[j+1])
{

}
}
}

for ( int i = 0; i < 5; i ++)
{
printf("%d ", array[i]);
}
return 0;

}```

2. Start your loops like this.
Code:
```     for ( int i = 0; i < 5; i ++)
{
for ( int j = i+1; j < 5; j++)```
There's no point searching before where you've already got to.

Then you're just comparing array[i] == array[j]

3. Originally Posted by Salem
Code:
```     for ( int i = 0; i < 5; i ++)
{
for ( int j = i+1; j < 5; j++)```
There's no point searching before where you've already got to.

Then you're just comparing array[i] == array[j]
Thank you

Code:
``` #include<stdio.h>
int main()
{
int array[5] = { 1, 2, 1, 2, 5};

for ( int i = 0; i < 5; i ++)
{
for ( int j = i+1; j < 5; j++)
{
if ( array[i] == array[j+1])
{
array[j+1] =0;

}
}
}

for ( int i = 0; i < 5; i ++)
{
printf("%d ", array[i]);
}
return 0;

}```
output

Code:
` 1 2 0 0 5`

4. Close, but you're stepping off the end of the array.
Code:
```#include<stdio.h>
int main()
{
int array[5] = { 1, 2, 1, 2, 5};

for ( int i = 0; i < 5; i ++)
{
for ( int j = i+1; j < 5; j++)
{
if ( array[i] == array[j])
{
array[j] =0;
}
}
}

for ( int i = 0; i < 5; i ++)
{
printf("%d ", array[i]);
}
return 0;
}```