function findMostFrequentElements(arr){
const freqMap = {};
let maxFreq = 0;
const result = [];
//统计每个元素出现的次数以及次数最多元素的个数
for(let i = 0 ; i < arr.length ; i++){
const elem = arr[i];
freqMap[elem] = (freqMap[elem] || 0) + 1;
maxFreq = Math.max(maxFreq , freqMap[elem]);
}
for( const j in freqMap){
if(freqMap[j] == maxFreq){
result.push(parseInt(j))
}
}
return result;
}
console.log(findMostFrequentElements([1,3,5,7,1,4,5,6]))//[1,5]
function splitString(arr){
if(arr.length%2 !== 0){
return false;
}
const vowels = new Set(['a','e','i','o','u','A','E','I','O','U']);
const halfLength = arr.length /2;
let aHalf = 0;
for( let i = 0;i<halfLength ;i++){
if(vowels.has(arr[i])){
aHalf++;
}
}
let bHalf = 0;
for(let j = halfLength; j<arr.length ; j++ ){
if(vowels.has(arr[j])){
bHalf++;
}
}
return aHalf == bHalf;
}
console.log(splitString('book'));//true
console.log(splitString('MerryChristmas'))//false
function reverseNumber(num){
let reversed = 0;
let sign = num >= 0 ? 1: -1;
num = Math.abs(num);
console.log("num",num);
var n = num + "";
reversed = parseInt(n.split("").reverse().join(""));
console.log("reversed",reversed)
return reversed*sign
}
console.log(reverseNumber(-64))//-46
console.log(reverseNumber(120))//21
function isValid(arr){
const stack = [];
const map = {
'(':')',
'{':'}',
'[':']'
};
for(let i = 0; i<arr.length ; i++){
if(arr[i] === '(' || arr[i] === '[' || arr[i] === '{' ){
stack.push(arr[i]);
}else {
const last = stack.pop();
if(arr[i] !== map[last]){
return false;
}
}
}
return stack.length === 0;
}
console.log(isValid("([)]"))//false
这个就不会了
#前端笔试##算法题#