C++ Program to remove duplicate numbers in an array

C++ Program to remove duplicate numbers in an array

C++ Program to remove duplicate numbers in an array

Object: Write a program that takes numbers as input and returns the result with all the duplicate numbers removed For example, if the input Array is {0, 1, 2, 2, 3, 4, 5, 6, 8, 8, 9, 1, 2}, the output will be {0,1, 2, 3, 4, 5, 6, 8, 9}. 

Code:

void main()
{
int i, j, k, n, num[100];

cout<<“How many Numbers Your Enter?\n”; cin>>n;
cout << “Enter Any “<> num[i];
}
for (i = 0; i < n; ++i)
for (j = i + 1; j < n;)
{
if (num[i] == num[j])
{
for (k = j; k < n – 1; ++k)
num[k] = num[k + 1];
–n;
}
else
++j;
}
cout << “\nDuplicate Numbers removed. \n”;
for (i = 0; i < n; ++i)
cout << num[i] << ” “;

getch();

}

#include<iostream>
#include <conio.h>

using namespace std;

void main()
{
	int i, j, k, n, num[100];
	cout<<"How many Numbers Your Enter?\n";
	cin>>n;
	cout << "Enter Any "<<n<<" Numbers :\n";
	for (i = 0; i < n; ++i)
	{
		cin >> num[i];
	}
	for (i = 0; i < n; ++i)
		for (j = i + 1; j < n;)
		{
			if (num[i] == num[j])
			{
				for (k = j; k < n - 1; ++k)
					num[k] = num[k + 1];
				--n;
			}
			else
				++j;
		}
	cout << "\nDuplicate Numbers removed. \n";
	for (i = 0; i < n; ++i)
		cout << num[i] << " ";

	getch();
}

Output:

C++ Program to remove duplicate numbers in an array
C++ Program to remove duplicate numbers in an array
No.C++ Projects for beginners with Source Code
1Write a C++ program to check triangle by entering 3 angles
2Write a C++ program that asks for the number of units sold and computes the total cost of the purchase.
3Write a program that asks the user to enter a number of seconds.
4Write a C++ program that will ask user to enter two integral numbers. The numbers should be swapped without using any third variable.
5Write a C++ program which takes the price of bakery items ranging from 0-999
6Write a C++ Program to print the bill.

Leave a Comment

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