C++ Program to Remove Duplicate Elements from Array

This is a C++ Program to Delete Repeated Elements.

Problem Description

The program takes an array, checks it for repeated elements and deletes them.

Problem Solution

1. The program takes an array.
2. Using for loops, the array is checked for repeated elements.
3. The rest of the elements are copied into another array,while the repeated ones are not.
4. The result is printed.
5. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Delete Repeated Elements. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int A[10], B[10], n, i, j, k = 0;
  6.     cout << "Enter size of array : ";
  7.     cin >> n;
  8.     cout << "Enter elements of array : ";
  9.     for (i = 0; i < n; i++)
  10.         cin >> A[i];    
  11.     for (i = 0; i < n; i++)
  12.     {
  13.         for (j = 0; j < k; j++)
  14.         {
  15.             if (A[i] == B[j])
  16.                 break;
  17.         }
  18.         if (j == k)
  19.         {
  20.             B[k] = A[i];
  21.             k++;
  22.         }
  23.     }
  24.     cout << "Repeated elements after deletion : ";
  25.     for (i = 0; i < k; i++)
  26.         cout << B[i] << " ";
  27.     return 0;
  28. }
Program Explanation

1. The user is asked to enter the array size and stored in the variable ‘n’.
2. The array elements are stored in the array ‘A’.
3. Using nested for loop, elements of A are copied into another array ‘B’, while the repeated elements are not.
4. Hence the array B does not contain any repeated elements. It is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter size of array : 5
Enter elements of array : 2 0 6 1 2
After deletion of repeated elements : 2 0 6 1
 
Case 2 :
Enter size of array : 3
Enter elements of array : 1 1 1
After deletion of repeated elements : 1
 
Case 3 :
Enter size of array : 10
Enter elements of array : 1 1 2 2 3 3 4 4 5 5
After deletion of repeated elements : 1 2 3 4 5

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.