Skip to main content

Sort Features by Popularity

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 276 ms)​

MetricValue
Runtime276 ms
Memory50 MB
Date2022-01-24
Solution
public class Solution {
public string[] SortFeatures(string[] features, string[] responses) {
Dictionary<string, int> freq = new Dictionary<string, int>();
foreach(var word in features)
{
freq.Add(word, 0);
}
foreach (var response in responses)
{
HashSet<string> arr = new HashSet<string>(response.Split(' '));
foreach (var res in arr)
{
if(freq.ContainsKey(res))
{
freq[res]++;
}
}
}
return freq.OrderByDescending(x => x.Value).Select(x => x.Key).ToArray();
}
}

Complexity Analysis​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed