
g0301_0400.s0347_top_k_frequent_elements.Solution.ex Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
# #Medium #Top_100_Liked_Questions #Array #Hash_Table #Sorting #Heap_Priority_Queue #Counting
# #Divide_and_Conquer #Quickselect #Bucket_Sort #Data_Structure_II_Day_20_Heap_Priority_Queue
# #Big_O_Time_O(n*log(n))_Space_O(k) #2024_08_04_Time_288_ms_(100.00%)_Space_93.7_MB_(100.00%)
defmodule Solution do
@spec top_k_frequent(nums :: [integer], k :: integer) :: [integer]
def top_k_frequent(nums, k) do
nums
|> Enum.frequencies()
|> Enum.sort(fn {_, x}, {_, y} -> x > y end)
|> Enum.take(k)
|> Enum.map(fn {n, _} -> n end)
end
end
© 2015 - 2025 Weber Informatics LLC | Privacy Policy