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

One thought on “C/C++ Program to Remove Duplicate Elements From Array

  1. Faiz

    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;
    }

    Reply

Leave a Reply

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