Q:

C Program to delete duplicate elements from array

0

C Program to delete duplicate elements from array

All Answers

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

#include <stdio.h>

#define MAX_SIZE 100 
int main()
{
    int arr[MAX_SIZE]; 
    int size;          
    int i, j, k;      

  
    printf("Enter size of the array : ");
    scanf("%d", &size);

   
    printf("Enter elements in array : ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &arr[i]);
    }


    
    for(i=0; i<size; i++)
    {
        for(j=i+1; j<size; j++)
        {
           
            if(arr[i] == arr[j])
            {
               
                for(k=j; k<size; k++)
                {
                    arr[k] = arr[k + 1];
                }

                /* Decrement size after removing duplicate element */
                size--;

                /* If shifting of elements occur then don't increment j */
                j--;
            }
        }
    }


    
    printf("\nArray elements after deleting duplicates : ");
    for(i=0; i<size; i++)
    {
        printf("%d\t", arr[i]);
    }

    return 0;
}

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now