Deletes the duplicate values from the list using Dynamically memory Allocates.

 Write a program that dynamically allocates memory to store n number of integers and deletes the duplicate values from the list. For example, if the list contains - 10, 6, 2, 10, 9; after deleting duplicate elements you will get the list - 10, 6, 2, 9. You must write a function, called DeleteDuplicate (int n, int *p), where n is the total array size and p is a pointer variable that have array address to solve the problem.


Code: 

#include <stdio.h>

void DeleteDuplicate(int n, int *p){
    printf("The array elements before removing duplicates: ");
    for(int i=0;i<n;i++)
        printf("%d ",p[i]);
    printf("\n");
    for(int m=0;m<n;m++)
    {

        for(int o=m+1;o<n;o++)
        {

            if(p[o]==p[m])
            {

                for(int k=o;k<(n-1);k++)
                {

                    p[k]=p[k+1];

                    n--;
                }

                o--;
            }
        }
    }
     printf("The array elements before removing duplicates: ");

    for(int i=0;i<n;i++)

        printf("%d ",p[i]);
}
int main()
{

    int array[] = {10, 6, 2, 10, 9};

    DeleteDuplicate(sizeof(array)/sizeof(array[0]),array);
    //return 0
    return 0;
}

Run:




Previous Post Next Post