C++ Program to Delete Duplicates Element from Array

Write C++ Program to Delete Duplicates Element from Array

// CPP Program to Delete Duplicates Element from Array

#include <iostream>

using namespace std;

int main()
{
	int A[] = {1, 2, 6, 16, 58, 7, 7, 777}, n, i, j, k, l, m;

	n = sizeof(A) / sizeof(A[0]);

        cout << "Array A :--> ";
	for(i=0; i<n; i++)
        cout << A[i] << " ";



	for(i = 0; i < n; i++)
	{
		for(j = i+1; j < n-1; j++)
		{
			if(A[i] == A[j])
			{
				for(k = j; k < n; k++)
				{
					A[k] = A[k+1];
				}
				j--;
				n--;
			}
		}
	}

        cout << "\nUpdated Array :--> ";
	for(i = 0; i < n ;i++)
	{
		cout << A[i] << " ";
	}

	return 0;
}

Output:

Run 1:
Array A :--> 1  2  6  6  58  7  7  777
Updated Array :--> 1 2 6 58 7 777

Run 2:
Array A :--> 1  2  6  16  58  17  7  777
Updated Array :--> 1 2 6 16 58 17 7 777

≪ Previous | Next ≫