The topkFrequent function will return the top k most frequent elements in an array

Finding Most Frequent Elements in an Array

Problem Statement We have to implement a topKFrequent function that takes an integer array and a value k as input and returns the k most frequent elements in the input array. For example, topKFrequent({1, 1, 2, 2, 3, 5, 2}, 2) will return the top 2 most frequent elements in the array i.e. {2, 1}. If multiple elements have the same frequency, then any of them could be returned in the solution as long as its length is k....

October 26, 2023 · 6 min · Avnish
The groupAnagrams function will return an array with anagrams grouped together

Group Anagrams in an Array

Problem Statement We have to implement a groupAnagram function that takes an array of strings as input and returns a new array with anagrams grouped. The input string array is assumed to be composed entirely of lowercase English characters. Brute Force Solution If two strings are anagrams then their sorted order will be the same. Thus, anagrams could be grouped under their sorted order. The brute-force implementation of groupAnagram uses a hashmap for grouping....

October 22, 2023 · 7 min · Avnish