The coding language is C++ Problem P6.10. • Write a function that removes duplic
ID: 3724368 • Letter: T
Question
The coding language is C++
Problem P6.10. •
Write a function that removes duplicates from a vector. For example, if remove_duplicates is called with a vector containing
1 4 9 16 9 7 4 9 11
then the vector is changed to
1 4 9 16 7 11
LHomework_ 5.pdf CSecure https://ccle.ucla.edu/pluginfile.php/2230142/mod_ resource/content/O/Homework 5.pdf » Submit the solution as hmw.5-3.cpp » Sample input-output: CC:Windows system321cmd.exe Enter a list of integers123 4 5 The list without dupliacates: 1 23 45 Continue ? y Enter a list of integers: 1 49 1697 49 11 The list without dupliacates 1 4 9 16 7 11 Continue y/n )? n Press any key to continue . 43 PM 3/5/2018 O Type here to search 41)Explanation / Answer
#include<iostream>
#include<vector>
using namespace std;
void remove_duplicates(vector<int> &v) //function to remove duplicates from vector
{
int arr[100005]={0}; //array having value 0 for every index
vector<int>a; //another vetor to store only distinct numbers
vector<int>::iterator it; //iterator for traversing vector
for(it=v.begin();it!=v.end();it++)
{
if(arr[*it]==0)
{
arr[*it]=1; //making index of the element 1 so that we know it has been encountered
a.push_back(*it); //pushing value to new array
}
else
{
continue;
}
}
v=a; //making the vector equal to the new vector conaining only non-duplicate elements
for(it=v.begin();it!=v.end();it++)
{
cout<<*it<<" ";
}
cout<<" ";
}
int main()
{
while(1)
{
vector<int> vec;
cout<<"Enter a the size of list of integers to input . ";
int siz;
cin>>siz;
cout<<"Enter a list of integers . ";
for(int i=0;i<siz;i++)
{
int d;
cin>>d;
vec.push_back(d);
}
cout<<"List without duplicate integers is "<<" ";
remove_duplicates(vec);
cout<<"Do you want to continue y/n ";
char c;
cin>>c;
if(c=='y'||c=='Y')
{
continue;
}
else{
break;
}
}
}