[#3005] feat(leetcode): init project, add C# solution
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// LeetCode 3005: Count_elements_with_maximum_frequency
|
||||
// 難度: Easy
|
||||
// 日期: 2025-09-22
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class Solution {
|
||||
public int MaxFrequencyElements(int[] nums)
|
||||
{
|
||||
Dictionary<int, int> frequencyMap = new Dictionary<int, int>();
|
||||
|
||||
// put nums into dictionary map
|
||||
foreach (var num in nums)
|
||||
{
|
||||
if (!frequencyMap.ContainsKey(num))
|
||||
{
|
||||
frequencyMap.Add(num, 0);
|
||||
}
|
||||
frequencyMap[num]++;
|
||||
}
|
||||
|
||||
// find most frequency nums
|
||||
int maxFrequency = 0;
|
||||
if (frequencyMap.Count != 0)
|
||||
{
|
||||
maxFrequency = frequencyMap.Values.Max();
|
||||
}
|
||||
|
||||
// count if have same max frequency nus
|
||||
int count = 0;
|
||||
foreach (var dic in frequencyMap)
|
||||
{
|
||||
if (dic.Value == maxFrequency)
|
||||
{
|
||||
count += maxFrequency;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public class Program {
|
||||
public static void Main() {
|
||||
var s = new Solution();
|
||||
// TODO: 可加入簡單輸入/輸出測試
|
||||
Console.WriteLine("Hello LeetCode 3005!");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user