[Mảng 1 Chiều Cơ Bản]. Bài 29. Mảng đánh dấu 4

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem source:
28Tech
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng số nguyên A[] gồm N phần tử, hãy tìm giá trị có số lần xuất hiện nhiều nhất trong mảng, nếu có nhiều giá trị có cùng số lần xuất hiện thì lấy số có giá trị nhỏ nhất

Tham khảo thêm lý thuyết mảng đánh dấu : [C++]. Mảng Đánh Dấu


Đầu vào

Dòng đầu tiên là số nguyên dương N

Dòng thứ 2 gồm N số nguyên viết cách nhau một vài khoảng trắng


Giới hạn

1<=N<=1000

0<=A[i]<=10^6


Đầu ra

In ra giá trị có số lần xuất hiện nhiều nhất kèm theo tần suất của nó


Ví dụ :

Input 01
5
9 4 0 4 5
Output 01
4 2
Input 02
5
1 2 3 4 5
Output 02
1 1

Comments

Please read the guidelines before commenting.



  • 0
    hngoc  commented on July 4, 2025, 3:41 p.m.
    n=input()
    a=input().split()
    b={}
    tt=[]
    for x in a:
        if not(x in b):
            b[x]=1
            tt.append(x)
        else: b[x]+=1
    tt.sort()
    ans=['', 0]
    for x in tt:
        if b[x]>ans[1]:
            ans[0]=x
            ans[1]=b[x]
    print(ans[0], ans[1])
    

  • 0
    aduangzi  commented on June 28, 2025, 2:27 p.m.

    include <bits/stdc++.h>

    using namespace std; using ll = long long;

    int main(){ iosbase::syncwithstdio(false); cin.tie(nullptr); freopen("vao.inp","r",stdin); freopen("ra.out","w",stdout); int n; cin >> n; map<int,int> mp; pair<int,int> v (INTMIN,INT_MIN); while (n--) { int x; cin >> x; mp[x]++; } for (auto x : mp) { if (x.second > v.second) { v.second = x.second; v.first = x.first; } else if (x.second == v.second) if (v.first > x.first) { v.first = x.first; } } cout << v.first << " " << v.second; return 0; }


  • 0
    B2405024  commented on April 19, 2025, 7:41 a.m.

    include<stdio.h>

    int main(){ int a[1000], b[1000]={0}, max=-1000000; int n; scanf("%d", &n); for(int i =0; i<n; i++) scanf("%d", &a[i]); for(int i=0; i<n; i++){ b[a[i]]++; if(a[i]>max) max = a[i]; } int m=0, c; for(int i=0; i<=max; i++){ if(b[i]){ if(b[i]>m){ m=b[i]; c=i; } } } printf("%d %d", c, m);

    }


  • 0
    minhnhatdz  commented on Jan. 26, 2025, 4:07 a.m.

    include <bits/stdc++.h>

    using namespace std; int a[1000001]; int dd[1000001]; int main() { int n;cin>>n; int m=0; for(int i=0;i<n;i++){ cin>>a[i]; dd[a[i]]++; m=max(dd[a[i]],m); } for(int i=0;i<1000001;i++){ if(dd[i]==m){ cout<<i<<" "<<m; break; } } }


  • 2
    NhatPham_123  commented on Jan. 23, 2025, 2:41 p.m.

    using namespace std; long long m[1000001] = {0}; long long n; int main(){ cin >> n; long long a[n]; for(int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for(int i = 0; i < n; i++){ m[a[i]] ++; } int res = 0; for(int i = 0; i < n; i++){ if(res < m[a[i]]) res = m[a[i]]; } for(int i = 0; i < n; i++){ if(res == m[a[i]]){ cout << a[i] << ' ' << m[a[i]]; break; } } return 0; }


  • 1
    NhatPham_123  commented on Jan. 23, 2025, 2:40 p.m.

    include<iostream>

    include<algorithm>

    using namespace std;

    long long m[1000001] = {0}; long long n; int main(){ cin >> n; long long a[n]; for(int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for(int i = 0; i < n; i++){ m[a[i]] ++; } int res = 0; for(int i = 0; i < n; i++){ if(res < m[a[i]]) res = m[a[i]]; } for(int i = 0; i < n; i++){ if(res == m[a[i]]){ cout << a[i] << ' ' << m[a[i]]; break; } } return 0; }


  • 3
    VDev  commented on Jan. 20, 2025, 1:02 p.m.

    FULL AC

    #include <bits/stdc++.h>
    #include <iomanip>
    #include <cmath>
    #include <climits>
    #define ll long long
    using namespace std;
    ll cnt = 0, s = 0, cnt2[10000011], a[10000011], res = INT_MIN;
    ll val;
    int main(){
        bool check = true;
        ll n;
        cin >> n;
        for(ll i = 1; i <= n; i++){
            cin >> a[i];
        }
        sort(a, a + n);
        for(ll i = 1; i <= n; i++){
                cnt2[a[i]]++;
        }
        for(ll i = 1; i <= n; i++){
            if(cnt2[a[i]] > res){
                res = cnt2[a[i]];
                val = a[i];
            }
        }
        for(ll i = 1; i <= n; i++){
                if(cnt2[a[i]] == cnt2[a[i + 1]]){
                    check = true;
                }else{
                    check = false;
                    break;
                }
        }
        for(ll i = 1; i <= n; i++){
            if(cnt2[a[i]] != 0 && check == true){
                if(cnt2[a[i]] > res){
                    res = cnt2[a[i]];
                    val = a[i];
                    break;
                }
            }
        }
        cout << val << " " << res;
        return 0;
    }
    

  • -9
    HoangChien2005  commented on Aug. 27, 2024, 2:46 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 7
      theguy777_jaboi  commented on Sept. 11, 2024, 12:09 p.m.

      đây ko phải môn đạo lý


    • 1
      theguy777_jaboi  commented on Sept. 11, 2024, 12:08 p.m.

      đang nói gì vậy


  • -42
    minhtran1482003  commented on April 22, 2024, 2:47 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.