陣列去重,打亂陣列,統計陣列各個元素出現的次數, 字串各個字元的出現次數,獲取地址連結的各個引數
以後會記錄自己解決過和遇到過的演算法相關的題,系列一就以常見的開篇吧。
陣列去重
- 遍歷陣列法
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);
複製程式碼
- 陣列下標判斷法
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);
複製程式碼
- 物件鍵值對法
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);
複製程式碼
- 排序後相鄰去除法
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);
複製程式碼
- 優化遍歷陣列法
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);
複製程式碼
- 利用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);
複製程式碼
- 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);
複製程式碼
統計陣列各個元素出現的次數
- 自創
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);
複製程式碼
- 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);
複製程式碼