The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Remove Duplicate Element From an Array

C Finding Duplicate Element in an Array

Here your task to collect any number of elements in an array and you have to display an array with non duplicate elements.

C Program - Remove Duplicate Elements From an Array

Let us write our own c program to Remove Duplicate Elements From An Array

remove-duplicate-element-from-array.c
#include <stdio.h>
int main()
{
int i, j, k, flag = 0, a[10], n;
printf("Enter array size: ");
scanf("%d",&n);
for(i = 0;i < n;i++)
scanf("%d ",&a[i]);
printf("\n");
for(i = 0;i < n;i++)
{
for(j = i-1;j >= 0;j--)
{
if(a[i] == a[j])
{
flag = 1;
}
}
if(flag == 0)
{
printf("%d\t",a[i]);
}
flag = 0;
}
return 0;
}
Enter array size : 6
1 1 2 3 4 5
1 2 3 4 5

Note:

The above program looks very simple, here goes the logic

  • First element will never be a duplicate element.
  • Compare second element with first element for duplicate check, if it is not a duplicate element then print else skip.
  • Compare third element with first and second element for duplicate check, if it is not a duplicate element then print else skip.
  • Repeat the step until you reach the last element in an array.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report