C/C++ Program to Remove Duplicate Elements From Array

Here you will get C and C++ program to remove duplicate elements from array.

 

For example:
Given Array: 5 8 9 5 12 9
New Array: 5 8 9 12

In this program I have compared each element with all other elements. If the element is equal to any other element in the array then shift all the elements to left by one place. The process is repeated till all the elements are compared. 

C/C++ Program to Remove Duplicate Elements From Array

C Program

C++ Program

 

Output

How many elements?5

Enter elements of array
12 4 6 4 2

12 4 6 2

3 thoughts on “C/C++ Program to Remove Duplicate Elements From Array”

  1. Another solution:

    #include
    #include

    int Check(std::vector &array, int num){
    int flag = 1;

    for (int i = 0; i < array.size(); i++){
    if (num == array[i]){
    flag = 0;
    break;
    }
    }
    return flag;
    }

    void PrintArray(std::vector &array){
    for (int i = 0; i < array.size(); i++)
    std::cout << array[i] << " ";
    }

    void RemoveDuplicates(std::vector &array){
    std::vector array_no_duplictaes;
    array_no_duplictaes.push_back(array[0]);

    for (int i = 1; i < array.size(); i++){
    if (Check(array_no_duplictaes, array[i]))
    array_no_duplictaes.push_back(array[i]);
    }

    PrintArray(array_no_duplictaes);
    }

    int main(){
    int num;
    std::vector array;
    std::cout <> num;
    if (num != -999)
    array.push_back(num);
    }
    RemoveDuplicates(array);
    std::cout << std::endl;

    return 0;
    }

  2. check this out!
    #include

    int main()
    {
    int i,j,k,n,a[30],count=0;
    printf(“How many elements?”);
    scanf(“%d”,&n);
    printf(“\nEnter elements of array\n”);

    for(i=0;i<n;++i)
    scanf("%d",&a[i]);

    for(i=0;i<n;++i)
    {
    k=i;

    for(j=i+1;j<n;j++)
    {
    if(a[i]!=a[j])
    {
    k++;
    a[k]=a[j];
    }
    else
    count++;
    }
    n=n-count;
    count=0;
    }

    printf("\n");
    for(i=0;i<n;++i)
    printf("%d ",a[i]);

    return 0;
    }

Leave a Comment

Your email address will not be published. Required fields are marked *