from collections import Counter
class Solution:
def sortFeatures(self, features, responses):
# Count the number of responses each feature appears in
feature_count = Counter()
for response in responses:
seen = set()
for word in response:
if word in features:
seen.add(word)
for word in seen:
feature_count[word] += 1
# Sort features by count (descending), then by their order in original features
feature_index = {feature: i for i, feature in enumerate(features)}
features_sorted = sorted(features, key=lambda f: (-feature_count[f], feature_index[f]))
return features_sorted
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<string> sortFeatures(vector<string>& features, vector<vector<string>>& responses) {
unordered_map<string, int> count;
for (auto& response : responses) {
unordered_set<string> seen;
for (auto& word : response) {
if (find(features.begin(), features.end(), word) != features.end())
seen.insert(word);
}
for (auto& word : seen) count[word]++;
}
unordered_map<string, int> featureIndex;
for (int i = 0; i < features.size(); ++i) featureIndex[features[i]] = i;
sort(features.begin(), features.end(), [&](const string& a, const string& b) {
if (count[a] != count[b]) return count[a] > count[b];
return featureIndex[a] < featureIndex[b];
});
return features;
}
};
import java.util.*;
class Solution {
public List<String> sortFeatures(List<String> features, List<List<String>> responses) {
Map<String, Integer> count = new HashMap<>();
for (List<String> response : responses) {
Set<String> seen = new HashSet<>();
for (String word : response) {
if (features.contains(word)) seen.add(word);
}
for (String word : seen) count.put(word, count.getOrDefault(word, 0) + 1);
}
Map<String, Integer> featureIndex = new HashMap<>();
for (int i = 0; i < features.size(); ++i) featureIndex.put(features.get(i), i);
features.sort((a, b) -> {
int cmp = Integer.compare(count.getOrDefault(b, 0), count.getOrDefault(a, 0));
if (cmp != 0) return cmp;
return Integer.compare(featureIndex.get(a), featureIndex.get(b));
});
return features;
}
}
var sortFeatures = function(features, responses) {
const count = {};
for (const response of responses) {
const seen = new Set();
for (const word of response) {
if (features.includes(word)) seen.add(word);
}
for (const word of seen) {
count[word] = (count[word] || 0) + 1;
}
}
const featureIndex = {};
for (let i = 0; i < features.length; ++i) {
featureIndex[features[i]] = i;
}
features.sort((a, b) => {
if ((count[b] || 0) !== (count[a] || 0)) return (count[b] || 0) - (count[a] || 0);
return featureIndex[a] - featureIndex[b];
});
return features;
};
features
(strings) and a list of responses
(each response is a list of strings), your task is to sort the features
in descending order of popularity. A feature's popularity is defined as the number of responses in which it appears at least once. If two features have the same popularity, maintain their original order from the input features
list.features
should be present in the output, sorted as described.features
list using a custom sorting key:
features = ["battery", "screen", "camera", "design"]
responses = [["battery", "camera", "battery"], ["screen", "design"], ["camera", "battery"], ["design", "screen", "screen"]]
["battery", "camera", "battery"]
:{"battery", "camera"}
["screen", "design"]
:{"screen", "design"}
["camera", "battery"]
:{"camera", "battery"}
["design", "screen", "screen"]
:{"design", "screen"}
["battery", "screen", "camera", "design"]