-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1772-sort-features-by-popularity.js
More file actions
50 lines (46 loc) · 1.62 KB
/
1772-sort-features-by-popularity.js
File metadata and controls
50 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 1772. Sort Features by Popularity
* https://leetcode.com/problems/sort-features-by-popularity/
* Difficulty: Medium
*
* You are given a string array features where features[i] is a single word that represents
* the name of a feature of the latest product you are working on. You have made a survey
* where users have reported which features they like. You are given a string array responses,
* where each responses[i] is a string containing space-separated words.
*
* The popularity of a feature is the number of responses[i] that contain the feature. You
* want to sort the features in non-increasing order by their popularity. If two features
* have the same popularity, order them by their original index in features. Notice that
* one response could contain the same feature multiple times; this feature is only counted
* once in its popularity.
*
* Return the features in sorted order.
*/
/**
* @param {string[]} features
* @param {string[]} responses
* @return {string[]}
*/
var sortFeatures = function(features, responses) {
const map = new Map();
for (const feature of features) {
map.set(feature, 0);
}
for (const response of responses) {
const uniqueWords = new Set(response.split(' '));
for (const word of uniqueWords) {
if (map.has(word)) {
map.set(word, map.get(word) + 1);
}
}
}
return features
.map((feature, index) => ({ feature, popularity: map.get(feature), index }))
.sort((a, b) => {
if (a.popularity !== b.popularity) {
return b.popularity - a.popularity;
}
return a.index - b.index;
})
.map(item => item.feature);
};