Skip to content
  • Sponsor wzhouwzhou/one-line-wonders

  • Notifications You must be signed in to change notification settings
  • Fork 190
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Duplicates from Array using C++ #366

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cplusplus/uniquearray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,a;
cin>>n;
vector <int> v;
for(int i=0;i<n;i++) //Take the vector
{ cin>>a; v.push_back(a); }

for (auto i=v.begin(); i!=v.end(); i++) //Remove duplicates
{ for(auto j=i+1;j!=v.end();j++)
{ if(*i==*j)
{ v.erase(j); j--; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v.erase(j--) ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that shortens the code. Now there is no need for braces coz it's a single statement. Sweet!

}
}

for (auto i=v.begin(); i!=v.end();i++) //Print the reult
cout<<*i<<" ";
cout<<endl;
}