17

排序,数组去重,打乱数组,统计数组各个元素出现的次数, 字符串各个字符的出现次数,获取地址链接的各个参数

以后会记录自己解决过和遇到过的算法相关的题,系列一就以常见的开篇吧。

排序

本来想多列几个排序方法,但是其它都不常见,更多请点击查看,冒泡排序和快速排序中以快速排序最优。

1. 冒泡排序
var arr = [0, 4, 2, 1, 3];
var i = j = 0;
for(i; i<arr.length-1; i++){
    for(j; j<=arr.length-i; j++){
        var temp = 0;
        // ">" 从小到大排序
        // "<" 从大到小排序
        if(arr[j] > arr[j+1]){
            temp = arr[j]
            arr[j] = arr[j+1]
            arr[j+1] = temp
        }
    }
}

console.log(arr);
2. 快速排序
function quickSort(arr){
    //如果数组<=1,则直接返回
    if (arr.length <= 1) { return arr; }
    var pivotIndex = Math.floor(arr.length/2);
    //找基准,并把基准从原数组删除
    var pivot = arr.splice(pivotIndex,1)[0];
    //定义左右数组
    var left = [];
    var right = [];

    //比基准小的放在left,比基准大的放在right
    for (var i=0;i<arr.length;i++) {
        if (arr[i] <= pivot) {
            left.push(arr[i]);
        } else {
            right.push(arr[i]);
        }
    }
    //递归
    return [...quickSort(left), pivot, ...quickSort(right)];
} 

console.log(quickSort([0, 4, 2, 1, 3]));

数组去重

1. 遍历数组法
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, newArr = [], len = arr.length; i < len; i++){
    if (newArr.indexOf(arr[i]) == -1) {
        newArr.push(arr[i]);
    }
}
console.log(newArr);
2. 数组下标判断法
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 1, newArr = [arr[0]], len = arr.length; i < len; i++){
    if (arr.indexOf(arr[i]) == i) {
        newArr.push(arr[i]);
    }
}
console.log(newArr);
3. 对象键值对法
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, obj = {}, newArr = [], len = arr.length, val, type; i < len; i++){
    val = arr[i];
    type = typeof val;
    if (!obj[val]) {  // 对象没有该键对应的值
        obj[val] = type;
        newArr.push(val);
    } else if (obj[val].indexOf(type) < 0) {//对象有该键对应的值且它的值是不重复的
        obj[val] = type;
        newArr.push(val);
    }
}
console.log(newArr);
4. 排序后相邻去除法
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
arr.sort();
var newArr = [arr[0]];
for (var i = 1, len = arr.length; i < len; i++) {
    if (arr[i] !== newArr[newArr.length-1]) {
        newArr.push(arr[i]);
    }
}
console.log(newArr);
5. 优化遍历数组法
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, len = arr.length, newArr = []; i < len; i++) {
    for (var j = i + 1; j < len; j++) {
        if (arr[i] === arr[j]) j = ++i;
    }
    newArr.push(arr[i]);
}
console.log(newArr);
6. 利用splice直接在原数组进行操作
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, len = arr.length; i < len; i++) {
    for (var j = i + 1; j < len; j++) {
        if (arr[i] === arr[j]) {
            arr.splice(j, 1);
            len--;
            j--;
        }
    }
}
console.log(arr);
7. es6简化版
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
// 7.1
var newArr = [...new Set(arr)]; 
console.log(newArr);

// 7.2
var newArr = Array.from(new Set(arr));
console.log(newArr);
// 数组去重,打乱数组,统计数组各个元素出现的次数, 字符串各个字符的出现次数,获取地址链接的各个参数

打乱数组

var arr = [2,5,'6',3,9,'2',4,7];
arr.sort(function(){ return 0.5 - Math.random() });
console.log(arr);

统计数组各个元素出现的次数

1. 自创
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, len = arr.length, newArr = []; i < len; i++) {
    var isPush = true;
    for (var j = 0, l = newArr.length; j < l; j++) {
        if (arr[i] === newArr[j].val) {
            isPush = false;
            newArr[j].count++;
        }
    }
    if (isPush) {
        newArr.push({val: arr[i], count: 1});
    } else {
        isPush = true;
    }
}
console.log(newArr);
2. es6
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
var newArr = arr.reduce((arrs, cv) => {
    const found = arrs.find(it => it[0] === cv);
    if (found) {
        found[1] += 1;
    } else {
        arrs.push([cv, 1]);
    }
    return arrs;
}, []);
console.log(newArr);

字符串各个字符的出现次数

var str = 'abdcsdwdaoprr';
for (var i = 0, len = str.length, obj = {}; i < len; i++) {
    if (obj[str[i]]) {
        obj[str[i]]++;
    } else {
        obj[str[i]] = 1;
    }
}
console.log(obj);

获取地址链接的各个参数

var url = 'https://www.google.com.hk/search?safe=strict&ei=M_u5WvLZO4bzUrfpn_AJ&q=js%9F%&oq=js%E7%BB&gs_l=psy-ab';
var search = url.substring(url.indexOf('?')+1);
    var tmpArr = search.split('&');
    var urlParmObj = {};
    if (tmpArr.length > 0 && tmpArr[0] != '') {
            for (var i = 0, len = tmpArr.length; i < len; i++) {
                    var tmp = tmpArr[i].split('=');
                    urlParmObj[tmp[0]] = tmp[1];
            }
    }
    console.log(urlParmObj);

es6 实践

var arr = [1,2,3,4,5];
const param = arr.find(it => it === 5);
param += 1;
console.log(arr);

更多其它内容请查看


xingqiwu
220 声望18 粉丝

茫茫人海中,茫茫地走着···