元素排序去重

排序去重
先是使用了C++中algorithm中设计好的sort算法,进行一个从小到大的排序,然后使用unique函数对这个数组进行一个整理。
unique函数做的事情就是把重复的元素放到后面去,实际上并没有把这些元素删除。
https://www.acwing.com/problem/content/description/5068/

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N=1e5+10;
    typedef vector<int>ve;
    ve a;
    
    int main()
    {
        
    int n;
    cin>>n;
    a.resize(n);
    for(int i=0;i<n;i++)
    cin>>a[i];
    
    sort(a.begin(),a.end());
    
    for(int i=0;i<n;i++)
    cout<<a[i]<<" ";
    cout<<endl;
    auto k=unique(a.begin(),a.end())-a.begin();
    for(int i=0;i<k;i++)
    cout<<a[i]<<" ";
    
    return 0;
    
    }


posted on 2024-05-01 22:31  不是小朋友L  阅读(4)  评论(0编辑  收藏  举报

导航