Q:

C Program For Remove Duplicates Items In An Array

belongs to collection: Array C Programs List

0

When the same number of elements occurs in sorted or unsorted array, then the elements of the array is called the duplicate elements. And we need to delete these duplicate elements or same number from an array to make the resultant array consists of unique elements.

For example, there is an integer type array arr[10] that contains { 5, 8, 3, 3, 5, 9} elements. In this array, 3 and 5 occurs two times. Hence, these elements are duplicate elements. So, after deleting the duplicate elements from an array arr[], we get 5, 8, 3,9 elements.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<stdio.h>
int main()
{
  int arr[50];
  int i,j,k,len;

  printf("Enter No. Of Element:\n");
  scanf("%d",&len);
  printf("Enter %d elements for the array:\n",len);
  for(i=0; i<len ; i++)
    scanf("%d",&arr[i]);

  for(i=0; i< len-1 ; i++){
    for(j=i+1; j<len; j++){

         if(arr[j] == arr[i]){
             k=j;
             while(k<(len-1))
             {
                 arr[k] = arr[k+1];
                 k++;
             }
             len--;
             j=i;
          }
      }
  }
  printf("The array after removing duplicates Elements :\n");

  for(i=0; i < len; i++)
    printf(" %d",arr[i]);

  return 0;
}

 

Output:

Enter No. Of Element:

6

Enter 6 elements for the array:

23

65

36

3

56

23

The array after removing duplicates Elements :

 23 65 36 3 56

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program To Delete Element From Array At Desired ... >>
<< C Program To Insert An Element Desired or Specific...