Top K Frequent Elements | Find k numbers with most occurrences in the given array | 3 Approaches

preview_player
Показать описание
In this tutorial, I have explained Top K Frequent Elements LeetCode Solution using Three Approaches.

Given a non-empty array of integers, return the k most frequent elements

For Example -

Example 1:

Input : {3, 4, 4, 4, 7, 7}, k = 2
Output : {4, 7}

Two most frequent elements are 4 and 7.


Example 2:

Input : {3}, k = 1
Output: {3}

LeetCode July Day 17 Challenge.

Find k numbers with most occurrences in the given array
Рекомендации по теме
Комментарии
Автор

Thank you sir, you explanation is very much helpful, your solution is clear and clever !

ser
Автор

In your FB post you mentioned O(n) approach but this is O(n log n) at best because of sorting which can't have worst case complexity less than O( n log n). So kindly don't mislead.

rajdeepghosh
Автор

In Your First Approach, it could have been better, if you have used TreeMap instead of creating a whole new List object just for Sorting. any thoughts on that !!

Anyway, I really like your video keep it up

dheerajvarma
Автор

Can u please explain the sorted(c1, c2) in code

surajgrandhi