Examples:
Input:
arr[ ] = {4, 0, 1, 8, 3, 4,3, 7, 0, 1}
Output:
4 0 1 8 3 7
Here,
Elements 0, 1,3 and 4 are present twice in the array.
Therefore, they are printed only once.
#include <bits/stdc++.h>
using namespace std;
void uniqueFun(int array[], int size)
{
// declare an empty hashset
unordered_set<int> ht;
// Traverse the input array
for (int i = 0; i < size; i++)
{
// if the current element is
// not present is ht
// then print it and put it in the table
if (ht.find(array[i]) == ht.end())
{
ht.insert(array[i]);
cout << "array : \n";
cout << array[i] << " ";
}
}
}
// Main function
int main()
{
int array[100], size;
cout << "Enter Number of elements: ";
cin >> size;
for (int i = 0; i < size; i++)
{
cin >> array[i];
}
uniqueFun(array, size);
return 0;
}
Output:
Enter Number of elements: 7
1 2 5 4 3 1 5
array : 1 2 5 4 3
0 Comments