Counting unique element in large array

前端 未结 8 719
执念已碎
执念已碎 2021-02-03 15:33

One of my colleagues was asked this question in an interview.

Given a huge array which stores unsigned int. Length of array is 100000000. Find the effective

8条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-03 16:05

    Look at its variation that might help you to find no. of distinct elements.

    #include 
    using namespace std;
    
    #define ll long long int
    #define ump unordered_map
    
    void file_i_o()
    {
    ios_base::sync_with_stdio(0); 
    cin.tie(0); 
    cout.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    }
    
    int main() {
    file_i_o();
    ll t;
    cin>>t;
    while(t--)
    {
        int n,q;
        cin>>n>>q;
        ump num;
        int x;
        int arr[n+1];
        int a,b;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            arr[i]=x;
            num[x]++;
        }
        for(int i=0;i>a>>b;
            num[arr[a]]--;
            if((num[arr[a]])==0)
            { num.erase(arr[a]); }
            arr[a]=b;
            num[b]++;
            cout<

提交回复
热议问题