MTFREQ - Đếm tần suất mảng

Xem dạng PDF

Gửi bài giải


Điểm: 1,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 256M

Tác giả:
Dạng bài
Ngôn ngữ cho phép
C, C#, C++, Go, Java, Pascal, Perl, PHP, PyPy, Python, Ruby, Rust, Scratch, Swift

Cho mảng ~A~ gồm ~n~ số nguyên không âm: ~a_1, a_2, …, a_n~, hãy đếm số lần xuất hiện của các phần tử khác nhau trong mảng.

Input

  • Dòng đầu ghi số nguyên dương ~n~ là số phần tử của mảng;
  • Dòng thứ hai ghi ~n~ số ~a_1, a_2, …, a_n~, mỗi số cách nhau bởi một dấu cách.

Giới hạn:

  • ~1 ≤ n ≤ 10^6, |a_i|≤ 10^9~.

Output

  • Dòng đầu ghi số nguyên dương ~m~ là số phần tử khác nhau trong mảng ~A~;
  • ~m~ dòng tiếp theo, mỗi dòng ghi ~2~ số ~u_i, f_i~ trong đó ~u_i~ là giá trị có trong mảng ~A, f_i~ là số lần xuất hiện của ~u_i~ (các số ~u_i~ được sắp xếp theo thứ tự xuất hiện lần đầu trong mảng ~A~).

Sample

Input #1
6
5 3 2 3 2 2
Output #1
3
5 1
3 2
2 3

Hint

Xét #1, có ~3~ giá trị khác nhau là ~5~, ~3~, ~2~ (theo đúng thứ tự xuất hiện). Số ~2~ xuất hiện ~3~ lần, số ~3~ xuất hiện ~2~ lần và số ~5~ xuất hiện ~1~ lần.

Problem source: Chuyên Sơn La Online Judge


Bình luận

Please read the guidelines before commenting.



  • 1
    mducc  đã bình luận lúc 3, Tháng 6, 2026, 2:57

    hint

    ta có thể lưu các phần tử chưa được đánh dấu rồi lưu vào 1 vector

    đáp án in ra là phần tử đó và tần xuất phần tử đó

    code tham khảo (c++):

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int n; 
        cin >> n; 
        int a[n + 1]; 
        map < int, int> mp;
        vector < int> id;  
        for(int i = 1; i <= n; ++i) {
            cin >> a[i]; 
            if (!mp[a[i]]) id.push_back(a[i]); 
            mp[a[i]]++; 
        }
        cout << id.size() << endl; 
        for(auto p : id) cout << p << " " << mp[p] << endl; 
    }
    

  • 0
    letranthanhkhuong  đã bình luận lúc 17, Tháng 2, 2026, 16:27 sửa 2

    include <bits/stdc++.h>

    define ll long long

    define endl '\n'

    define f firts

    define s second

    define pb push_back

    define fio iosbase::syncwith_stdio(false);cin.tie(nullptr);cout.tie(nullptr)

    using namespace std; unorderedmap<int,int> mp; vector<int> a; vector<int> tt; int main() { fio; int n; cin >> n; a.reserve(n); for(int i =0; i < n; i++) { int x; cin >> x; a.pb(x); } for(int x : a) { if(mp[x] == 0) tt.pushback(x); mp[x]++; } cout << mp.size() << endl; for(int x : tt) { cout << x << ' '<< mp[x] << endl; } }


  • -1
    nhankiettvt  đã bình luận lúc 24, Tháng 1, 2026, 2:08

    FULL AC CHO AE

    
    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    using ull = unsigned long long;
    
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(nullptr);
        int n; cin >> n;
        vector<int> a(n),pos;
        unordered_map<int,int> m;
        int dem=0;
        for (auto &i:a){
            cin >> i;
            if (m[i]==0){
                pos.push_back(i);
                dem++;
            }
            m[i]++;
        }
        cout << dem << "\n";
        for (int i : pos){
            cout << i << " " << m[i] << "\n";
        }
        return 0;
    }
    
    

  • 1
    congtyluuthaibao1978  đã bình luận lúc 26, Tháng 11, 2025, 5:16

    include <bits/stdc++.h>

    using namespace std; int main() { ios::syncwithstdio(false); cin.tie(nullptr);

    int n;
    cin >> n;
    vector&lt;long long> a(n);
    for(int i = 0; i < n; i++)
    {
        cin>>a[i];
    }
    unordered_map&lt;long long,int> cnt;
    vector&lt;long long> order;
    order.reserve(n);
    for(int i = 0; i < n; i++){
        long long x = a[i];
        if(cnt.find(x) == cnt.end()){
            // lần đầu thấy x
            order.push_back(x);
            cnt[x] = 1;
        } else {
            cnt[x]++;
        }
    }
    cout << order.size() <&lt;endl;
    for(long long x: order){
        cout << x << " " << cnt[x] <&lt;endl;
    }
    return 0;
    

    }


  • 1
    Nguyenhoang150908  đã bình luận lúc 12, Tháng 11, 2025, 4:13

    include <bits/stdc++.h>

    using namespace std;int main() {ios::syncwithstdio(false);cin.tie(nullptr);int n;cin>>n;vector<int>order;unorderedmap<int,int>freq;unorderedset<int>seen;for(int i=0;i<n;i++){int x;cin>>x;freq[x]++;if(!seen.count(x)){seen.insert(x);order.push_back(x);}}cout<<order.size()<<'\n';for (int x:order){cout<<x<<' '<<freq[x]<<'\n';}return 0;}