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

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 giá trị xuất hiện trước theo thứ tự trong mảng

Tham khảo 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
6
7 2 0 3 9 5
Output 01
7 1

Comments

Please read the guidelines before commenting.



  • 0
    bengokyeuanh99  commented on May 28, 2025, 2:58 p.m.

    !/usr/bin/env python3

    -- coding: utf-8 --

    from collections import defaultdict import sys

    def main(): input = sys.stdin.readline

    n = int(input())
    arr = list(map(int, input().split()))
    
    # Map: value -> (frequency, first_index)
    freq_map = defaultdict(lambda: [0, -1])
    
    for idx, val in enumerate(arr):
        freq_map[val][0] += 1
        if freq_map[val][1] == -1:
            freq_map[val][1] = idx
    
    # Tìm phần tử có frequency lớn nhất, nếu bằng nhau thì ưu tiên vị trí nhỏ hơn
    max_val = None
    max_freq = -1
    min_index = n + 1
    
    for val, (freq, first_idx) in freq_map.items():
        if freq > max_freq or (freq == max_freq and first_idx < min_index):
            max_freq = freq
            max_val = val
            min_index = first_idx
    
    print(f"{max_val} {max_freq}")
    

    if name == "main": main()


  • 0
    quynhcham416  commented on May 28, 2025, 12:57 p.m.

    import sys input=sys.stdin.readline n=int(input()) N=list(map(int,input().split())) def dd(a): dg=[] for i in a: if i not in dg: dg.append(i) return dg ff=dd(N) dem=len(ff) kq=lq=0 for i in range(dem): c=ff[i] if N.count(ff[i])>kq: lq=ff[i] kq=N.count(ff[i]) print(lq, kq)


  • 0
    htgiao404  commented on May 8, 2025, 6:23 a.m.

    include <bits/stdc++.h>

    using namespace std; using ll = long long;

    int cnt[1000005];

    int main(){ ios::syncwithstdio(false); cin.tie(nullptr); int n; cin >> n; int a[n]; for(int i = 0; i < n; i++){ cin >> a[i]; cnt[a[i]]++; } int maxtime = *maxelement(cnt, cnt + 1000005); for(int i = 0; i < n; i++){ if(cnt[a[i]] == max_time){ cout << a[i] << ' ' << cnt[a[i]]; break; } } }


  • 0
    VDev  commented on Jan. 20, 2025, 1:03 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];
        }
        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;
    }
    

  • 0
    minhhuy1604it  commented on Oct. 2, 2024, 3:17 p.m. edit 5

    Heading

    /#include < bits/stdc++.h>

    using namespace std;

    int b[1000001];

    int main(){

    int n,i;

    cin>>n;

    int a[n];

    for( i=0 ; i < n ; i++ ){

    cin>>a[i];

    b[a[i]]++; }

    int max=-1e9;

    for( i=0; i < n; i++){

    if(b[a[i]]>max){ max=b[a[i]]; }

    }

    for( i=0 ; i < n ; i++ ){

    if(b[a[i]]==max){

    cout<< a[i]<< " "<< max<< endl;

    break; }

    }

    return 0; }


    • 0
      minhhuy1604it  commented on Oct. 2, 2024, 3:33 p.m. edited

      Phần bình luận này khó share code vãi :(((

      Nhớ xóa dấu các trước bits/stdc++.h để chạy code nha mấy bạn


  • 7
    Zinno  commented on July 20, 2024, 1:46 p.m.

    GG, this is so hard lmao:)


    • -2
      duy210  commented on Sept. 3, 2024, 11:02 a.m.

      hard thật


  • -25
    phong2k12  commented on May 16, 2024, 2:04 p.m.

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