前端算法面试题整理

时间:2021-04-16 09:42:37

1.写一个方法将数组换成前端更易解析的树状结构

function getTree(data) {
    var newData = [],
        hash = {};
    for (var i = 0; i < data.length; i++) {
        if (!hash[data[i].province]) {
            hash[data[i].province] = {
                'province': data[i].province
            };
            hash[data[i].province]['city'] = [{
                'name': data[i].city,
                'code': data[i].code
            }]
            newData.push(hash[data[i].province]);
        } else if (hash[data[i].province].province == data[i].province) {
            hash[data[i].province]['city'].push({
                'name': data[i].city,
                'code': data[i].code
            })
        }
    }
    return newData;
}

var data = [{
    'province': '浙江',
    'city': '温州',
    'code': '10010'
}, {
    'province': '浙江',
    'city': '杭州',
    'code': '10011'
}, {
    'province': '安徽',
    'city': '合肥',
    'code': '10012'
}, {
    'province': '安徽',
    'city': '马鞍山',
    'code': '10013'
}, {
    'province': '浙江',
    'city': '宁波',
    'code': '10014'
}];
console.log(getTree(data));

  

 

2.统计字符串出现最多的字母

function getMax(str) {
    var hash = {},
        maxstr, max = 1;
    for (var i = 0; i < str.length; i++) {
        if (!hash[str[i]]) {
            hash[str[i]] = 1;
        } else {
            hash[str[i]]++;
        }
        if (hash[str[i]] > max) {
            max = hash[str[i]];
            maxstr = str[i];
        }
    }
    return maxstr;
}

var str = 'abcdeffggghhhhiiiii';
console.log(getMax(str));

 

 

 

3.排序算法

function bubbleSort(arr) {
    for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < arr.length - i - 1; j++) {
            if (arr[j] < arr[j + 1]) {
                var temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr;
}

var arr = [1, 2, 8, 3, 4, 1];
console.log(bubbleSort(arr));

 

 

4.去重算法

function unique1(arr) {
    var newArr = [];
    for (var i = 0; i < arr.length; i++) {
        if (newArr.indexOf(arr[i]) == -1) {
            newArr.push(arr[i]);
        }
    }
    return newArr;
}

var arr1 = ['b', 'b', 'a', 1, 3, 4, 4];
console.log(unique1(arr1));

function unique2(arr) {
    var hash = {},
        newArr = [];
    for (var i = 0; i < arr.length; i++) {
        if (!hash[arr[i]]) {
            hash[arr[i]] = true;
            newArr.push(arr[i]);
        }
    }
    return newArr;
}

var arr2 = ['b', 'b', 'a', 1, 3, 4, 4];
console.log(unique2(arr2));

function unique3(arr) {
    for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < arr.length; j++) {
            if (arr[i] === arr[j] && i != j) {
                arr.splice(i, 1);
            }
        }
    }
    return arr;
}

var arr3 = ['b', 'b', 'a', 1, 3, 4, 4];
console.log(unique3(arr3));

 

 

5.二分查找算法(建立在已经排好序的情况下)

function binarySearch(arr, data) {
    var end = arr.length - 1,
        start = 0;

    while (start <= end) {
        var middle = Math.floor((start + end) / 2);
        if (arr[middle] > data) {
            end = middle - 1;
        } else if (arr[middle] < data) {
            start = middle + 1;
        } else {
            return middle;
        }
    }
    return -1;

}

var arr = [1, 2, 3, 4, 5, 6];
console.log(binarySearch(arr, 2));