Files
Leetcode/347. Top K Frequent Elements/Solution.cs
Пытков Роман 9b3c8c3fef 347. Top K Frequent Elements
2024-02-07 23:27:37 +03:00

13 lines
390 B
C#

namespace _347._Top_K_Frequent_Elements;
public class Solution
{
public int[] TopKFrequent(int[] nums, int k)
{
var dictionary = new Dictionary<int, int>();
foreach (var num in nums)
if (!dictionary.TryAdd(num, 1))
dictionary[num]++;
return dictionary.OrderByDescending(kp=>kp.Value).Take(k).Select(kp=>kp.Key).ToArray();
}
}