mirror of
				https://git.wolves.top/wolves/leetcode.git
				synced 2025-11-04 09:16:32 +08:00 
			
		
		
		
	250923
This commit is contained in:
		
							
								
								
									
										35
									
								
								25/09/3005.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								25/09/3005.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <unordered_map>
 | 
			
		||||
 | 
			
		||||
int maxFrequencyElements(std::vector<int> &nums)
 | 
			
		||||
{
 | 
			
		||||
    std::unordered_map<int,int> m;
 | 
			
		||||
    std::for_each(nums.begin(),nums.end(),[&m](int x){
 | 
			
		||||
        m[x]++;
 | 
			
		||||
    });
 | 
			
		||||
    int max = 0;
 | 
			
		||||
    for_each(m.begin(),m.end(),[&max](std::pair<int,int> p){
 | 
			
		||||
        if (p.second > max) max = p.second;
 | 
			
		||||
    });
 | 
			
		||||
    int count = 0;
 | 
			
		||||
    std::for_each(m.begin(),m.end(),[&count,max](std::pair<int,int> p){
 | 
			
		||||
        if (p.second == max) count++;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    return count;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int enhance_1(std::vector<int> nums) {
 | 
			
		||||
    std::unordered_map<int,int> m;
 | 
			
		||||
    int ans = 0, max = 0;
 | 
			
		||||
    std::for_each(nums.begin(),nums.end(),[&m,&ans,&max](int x) {
 | 
			
		||||
        int c = ++m[x];
 | 
			
		||||
        if (c > max) {
 | 
			
		||||
            ans = max = c;
 | 
			
		||||
        }else if (c == max) {
 | 
			
		||||
            ans += c;
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
    return ans;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								25/09/3005.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								25/09/3005.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func maxFrequencyElements(nums []int) int {
 | 
			
		||||
	m := make(map[int]int)
 | 
			
		||||
	ans := 0
 | 
			
		||||
	max := 0
 | 
			
		||||
	for _, x := range nums {
 | 
			
		||||
		m[x]++
 | 
			
		||||
		c := m[x]
 | 
			
		||||
		if c > max {
 | 
			
		||||
			max = c
 | 
			
		||||
			ans = c
 | 
			
		||||
		} else if c == max {
 | 
			
		||||
			ans += c
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return ans
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	// 示例测试
 | 
			
		||||
	nums := []int{1, 2, 2, 3, 1, 4}
 | 
			
		||||
	fmt.Println(maxFrequencyElements(nums)) // 输出: 4
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								25/09/3005.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								25/09/3005.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
class Solution {
 | 
			
		||||
    public int maxFrequencyElements(int[] nums) {
 | 
			
		||||
        Map<Integer, Integer> map = new HashMap<>();
 | 
			
		||||
        int max = 0, ans = 0;
 | 
			
		||||
        for (int num : nums) {
 | 
			
		||||
            map.put(num, map.getOrDefault(num, 0) + 1);
 | 
			
		||||
            int c = map.get(num);
 | 
			
		||||
            if (c > max) {
 | 
			
		||||
                max = c;
 | 
			
		||||
                ans = c;
 | 
			
		||||
            } else if (c == max) {
 | 
			
		||||
                ans += c;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return ans;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user