-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5th.cpp
More file actions
36 lines (34 loc) · 916 Bytes
/
5th.cpp
File metadata and controls
36 lines (34 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//remove duplicates from an array
// (Method 2 at 7th.cpp file)
//I will use another array as hash to count the frequency
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter numeber of elements in an array :\n";
cin>>n;
int arr[n];
cout<<"Enter elements in the array :"<<endl;
int max_element=INT_MIN;
for(int i=0;i<n;i++)
{
cout<<": ";
cin>>arr[i];
if(arr[i]>max_element)
max_element=arr[i];
}
int *hash=new int[max_element];//create an array in heap
hash[0]=0;//creates an array with total size as one more than that of max element of array
//I will use this array to count the frequency
for(int i=0;i<n;i++)
{
hash[arr[i]]+=1;
}
cout<<"Array elements after removing duplicates are : \n";
for(int i=0;i<=max_element;i++)
{
if(hash[i]>0)
cout<<i<<" ";
}
}