I have an exercise that is asking me to make a vector, fill it with 20 integers from 1-20 and then print it. Next, check to find repeat integers and then remove them.
The method that I think that I am going to use is to find an integer, then check every integer in the vector. If there is another integer with the same value then I will get rid of that, then go to the next integer.
I'm really unsure of how to go about this. Any help is appreciated. Here's what I have so far, but it's not working.
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <time.h>
usingnamespace std;
int main() {
srand(time(NULL));
vector<int> List;
cout << "Here is Santa's list!\n";
for(int i = 0; i<20; i++){
List.push_back(rand() % 20 + 1);///Fills vector with 20 random integers
cout << List[i] << " ";///Prints the vector
}
vector<int> Revision(List.begin(), List.end());
cout << "\n Maybe Santa made a mistake! Here is the revised version. \n";
///Now, I should revise the list to remove repeat integers i.e. if there are multiple '4's, then skip the rest of the fours
for(int n = 0; n<20; n++){
for(int i = 0; i<20; i++){
if(Revision[i] == n && i == 1){
Revision.erase(Revision.begin());
}
if(Revision[i] == n){
Revision.erase(Revision.begin() + (i-1));///Removes that element
}
}
}
for(int i = 0; i<20; i++){///Prints the list
cout << Revision[i] << " ";
}
}