1. Js counts the letters/numbers that appear most frequently in a string

let str = 'asdfghjklaqwertyuiopiaia';
const strChar = str => {
    let string = [...str],
        maxValue = ' ',
        obj = {},
        max = 0;
    string.forEach(value => {
        obj[value] = obj[value] == undefined ? 1 : obj[value] + 1
        if (obj[value] > max) {
            max = obj[value]
            maxValue = value
        }
    })
return maxValue;
}
console.log(strChar(str))    // a
Copy the code

2. Array deduplication

2.1, forEach
let arr = ['1'.'2'.'3'.'1'.'a'.'b'.'b']
const unique = arr => {
    let obj = {}
    arr.forEach(value => {
        obj[value] = 0
    })
    return Object.keys(obj)
}
console.log(unique(arr))  // ['1'.'2'.'3'.'a'.'b']
Copy the code
2.2, the filter
let arr = ['1'.'2'.'3'.'1'.'a'.'b'.'b']
const unique = arr => {
    return arr.filter((ele, index, array) => {
        return index === array.indexOf(ele)
    })
}
console.log(unique(arr))  // ['1'.'2'.'3'.'a'.'b']
Copy the code
2.3, the set
let arr = ['1'.'2'.'3'.'1'.'a'.'b'.'b']
const unique = arr => {
    return [...new Set(arr)]
}
console.log(unique(arr))  // ['1'.'2'.'3'.'a'.'b']
Copy the code
2.4, reduce
let arr = ['1'.'2'.'3'.'1'.'a'.'b'.'b']
const unique = arr.reduce((map, item) => {
    map[item] = 0
    return map
}, {})
console.log(Object.keys(unique))  // ['1'.'2'.'3'.'a'.'b']
Copy the code

3. Flip the string

let str ="Hello Dog";
const reverseString = str =>{
    return [...str].reverse().join("");
}
console.log(reverseString(str))   // goD olleH
Copy the code

4. Maximum difference in array

4.1, forEach
let arr = [23, 4, 5, 2, 4, 5, 6, 6, 71, -3];
const difference = arr => {
    let min = arr[0],
        max = 0;
    arr.forEach(value => {
        if (value < min) min = value
        if (value > max) max = value
    })
    return max - min ;
}
console.log(difference(arr))  // 74
Copy the code
4.2. Max, min
let arr = [23, 4, 5, 2, 4, 5, 6, 6, 71, -3];
const difference = arr => {
    letmax = Math.max(... arr), min = Math.min(... arr);return max - min ;
}
console.log(difference(arr)) // 74
Copy the code

5. Exchange two integers without using temporary variables

5.1. Array deconstruction
let a = 2,
    b = 3;
    [b,a] = [a,b]
    console.log(a,b)   // 3 2
Copy the code
5.2 Arithmetic Operations (addition and subtraction)

Input a = 2,b = 3, output a = 3,b = 2

let a = 2,
    b = 3;
const swop = (a, b) => {
    b = b - a;
    a = a + b;
    b = a - b;
    return[a,b]; } the console. The log (swop (2, 3)) / / [3, 2]Copy the code
5.3 Logical Operation (XOR)
leta = 2, b = 3; const swop = (a, b) => { a ^= b; // select * from b ^= a; // a ^= b; // a ^= b; // a ^= b; // Leave y unchanged, use x to xor reverse the original value of y to equal the original value of yreturn[a,b]; } the console. The log (swop (2, 3)) / / [3, 2]Copy the code

6. Sort (from small to large)

6.1 bubble sort
let arr = [43, 32, 1, 5, 9, 22];
const sort = arr => {
    arr.forEach((v, i) => {
        for (let j = i + 1; j < arr.length; j++) {
            if (arr[i] > arr[j]) {
                [arr[i],arr[j]] = [arr[j],arr[i]]
            }
        }
    })
    return arr
}
console.log(sort(arr))  // [1, 5, 9, 22, 32, 43]
Copy the code

conclusion

Constantly updated, constantly updated…

Mistakes and omissions, please correct

There are more and better JS algorithm interview questions, but also hope that partners actively recommend