1st Code
var majorityFrequencyGroup = function(s) {
const freqMap = new Map();
for (const char of s) {
freqMap.set(char, (freqMap.get(char) || 0) + 1);
}
const groups = new Map();
for (const [char, freq] of freqMap.entries()) {
if (!groups.has(freq)) groups.set(freq, []);
groups.get(freq).push(char);
}
let bestSize = -1, bestFreq = -1;
let bestGroup = [];
for (const [freq, chars] of groups.entries()) {
const groupSize = chars.length;
if (groupSize > bestSize || (groupSize === bestSize && freq > bestFreq)) {
bestSize = groupSize;
bestFreq = freq;
bestGroup = chars;
}
}
return bestGroup.join('');
};
Join👇
@leetcode_cp
@leetcode_cp
Post #241
52