This is a C++ Program to Delete Repeated Elements.
The program takes an array, checks it for repeated elements and deletes them.
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.
Here is the source code of C++ Program to Delete Repeated Elements. The program output is shown below.
#include<iostream>
using namespace std;
int main ()
{
int A[10], B[10], n, i, j, k = 0;
cout << "Enter size of array : ";
cin >> n;
cout << "Enter elements of array : ";
for (i = 0; i < n; i++)
cin >> A[i];
for (i = 0; i < n; i++)
{
for (j = 0; j < k; j++)
{
if (A[i] == B[j])
break;
}
if (j == k)
{
B[k] = A[i];
k++;
}
}
cout << "Repeated elements after deletion : ";
for (i = 0; i < k; i++)
cout << B[i] << " ";
return 0;
}
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.
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.
- Apply for C++ Internship
- Apply for Computer Science Internship
- Check C++ Books
- Practice Programming MCQs
- Practice Computer Science MCQs