Javascript 數組經常使用算法

數組去重

filter 方法實現

// 0.0.2/arrayUniqueFilter.js
const arr = [1, 2, 11, 22, 11, 1, 11, 22, 1, 2];
const unique = arr => arr.filter((element, index, self) => {
    // self.indexOf(element) 每次都從數組第一個元素開始往下查
    return self.indexOf(element) === index;
});
console.log(unique(arr)); // [1, 2, 11, 22]

// 上面可簡寫爲
const unique2 = arr => arr.filter((element, index, self) => self.indexOf(element) === index);
console.log(unique2(arr)); // [1, 2, 11, 22]

// 元素非對象
const arr3 = [
    { id: 1, name: '張三' },
    { id: 2, name: '李四' },
    { id: 11, name: '王五' },
    { id: 1, name: '張三' },
    { id: 11, name: '王五' },
    { id: 3, name: '李四' }
];
const unique3 = (arr, id) => {
    let cache = [];
    for(let item of arr){
        if(cache.find(v => v[id] === item[id])) {
            continue;
        }
        cache.push(item);
    }
    return cache;
}
console.log(unique3(arr3, 'id')); // [{id: 1, name: "張三"}, {id: 2, name: "李四"}, {id: 11, name: "王五"}, {id: 3, name: "李四"}]
複製代碼

reducer 方法實現

// 0.0.2/arrayUniqueReducer.js
const unique = arr => arr.sort().reduce((cache, current) => {
    if(cache.length === 0 || cache[cache.length -1] !== current){
        cache.push(current);
    }
    return cache
}, []);

const arr = [1, 2, 11, 22, 11, 1, 11, 22, 1, 2];
console.log(unique(arr));
複製代碼

數組降維

// 0.0.2/arrayDimensionalityReduction.js
const arr = [1, [ 1, 2, [2, 3]], [3, 4], 5];
const arrayDimensionalityReduction = arr => Array.isArray(arr) ? 
    arr.reduce((prev, cur) => [...prev, ...arrayDimensionalityReduction(cur)], []) :
    [arr];

console.log(arrayDimensionalityReduction(arr)); // [1, 1, 2, 2, 3, 3, 4, 5]
複製代碼

數組交集

// 0.0.2/array_algorithms.js
const arr1 = [1, 7, 4, 5, 2, 1, 5, 3, 6, 2, 1, 3,];
const arr2 = [2, 4, 3, 4, 5, 5, 5];

const intersection = (arr1, arr2) => {
    const map = new Map();
    const arr = [];
    // 鍵值對,鍵是 value,值是 value 出現次數, 出現次數累加
    for (let i = 0; i < arr1.length; i++) {
        const value = arr1[i];
        let valueInMap = map.get(value);
        valueInMap = (valueInMap ? valueInMap : 0) + 1
        map.set(value, valueInMap)
    }
    // 鍵值對,中轉 map 判斷第二個數組的值是否存在,存在就留下來,而後 map 值次數減1
    for (let i = 0; i < arr2.length; i++) {
        const value = arr2[i];
        if (map.has(value) && map.get(value) !== 0) {
            arr.push(value);
            map.set(value, map.get(value) - 1);
        }
    }
    return arr;
}

console.log(intersection(arr1, arr2)); // [ 2, 4, 3, 5, 5 ]
複製代碼

數組和求下標

需求:給一個數組和一個數字,求數組中兩個值的和等於這個數字的下標。javascript

// 0.0.2/arraySumFindSub.js
const arr = [1, 2, 4, 8, 5];

const arraySum = (arr, target) => {
    const map = new Map();
    const newArr = [];
    for (let i = 0; i < arr.length; i++) {
        const value = arr[i];
        const difValue = target - value;
        if (map.has(difValue)) {
            newArr.push(map.get(difValue), i);
            break;
        }
        map.set(value, i)
    }
    return newArr;
}

console.log(arraySum(arr, 9)); // [0, 3]
複製代碼

本次代碼 Githubjava

你能夠...

相關文章
相關標籤/搜索