It has been a long time since the last post. Recently, I have been busy with my work. I don't think it's nearly double eleven. It's equivalent to giving you some benefits.
I. What is array de-duplication
Simply put, delete the duplicate items in the array. Have you got GET? Below I will briefly introduce several basic methods and their advantages and disadvantages.
II. Method Summary
- Two level circulation
** push directly into the new array without the same value, and skip the internal loop with the same value **
/* * @param {Array} arr -An array to be weighted * @param {Array} result -Initialization result array */ const unique = (arr, result = []) => { const len = arr.length; for (let i = 0; i < len; i++) { for (let j = i + 1; j < len; j++) { if (arr[i] === arr[j]) { // Equality is skipped directly j = ++i; } } result.push(arr[i]); } return result; }
** Mark the same, compare with the new array, insert ** if not
/* * @param {Array} arr -An array to be weighted * @param {Array} result -Initialization result array */ const unique = (arr, result = []) => { result.push(arr[0]); const len = arr.length; let rLen = result.length; for (let i = 1; i < len; i++) { let flag = false; for (var j = 0; j < rLen; j++) { if (arr[i] === result[j]) { flag = true; break; } } if (!flag) { rLen++; result.push(arr[i]); } } return result; }
** In-situ algorithm (operated on the array itself)**
const unique = arr => { const len = arr.length; for (let i = 0; i < len; i++) { for (let j = i + 1; j < len; j++) { if (arr[i] == arr[j]) { arr.splice(j,1); len--; j--; } } } return arr; };
It seems that the code code is simple, but the memory consumption is high and it is not practical.
- Single layer circulation
** Object keys cannot be repeated **
const unique = (arr, result = []) => { const obj = {}; const len = arr.length; for (let i = 0; i< len; i++) { if (!obj[arr[i]]) { // If there is no key, add obj[arr[i]] = 1; result.push(arr[i]); } } return result; };
This method can not judge the type of'1'and'1'. Solution:
- Add judgment data types, such as typeof, obj[typeof arr[i] + arr[i]], but that's still impossible to judge ['1'] and [1], because when added, the results are the same.
- When you add JSON.stringify() to de-format the result, you can judge it.
** After sorting, compare the two before and after, and add a new array if they are not equal **
const unique = (arr, result = []) => { arr.sort(); result.push(arr[0]); const len = arr.length; let rLen = result.length; for (let i = 1; i < len; i++) { if (arr[i] !== result[rLen - 1]) { result.push(arr[i]); rLen++; } } return result; }
The method is more direct.
** In-situ algorithm (comparing the two before and after sorting, deleting the same)**
const unique = (arr) => { arr.sort(); let len = arr.length; for (let i = 1; i < len; i++) { if (arr[i] === arr[i - 1]) { arr.splice(i, 1) len--; } } return arr; }
No extra space consumed
- Lazy rhythm
** indexOf to determine whether the first occurrence of an array element is the same **
const unique = (arr, result) => { arr.forEach((item, index, array) => { if(array.indexOf(item) === index) { result.push(item); } }); return result; } // Use ES6 filter const unique = (arr) => arr.filter((item, index) => array.indexOf(item) === index);
More concise and better performance using ES6 method
** The ES6 method of indexOf uses includes to determine whether or not the element is in the new array**
const unique = (arr, result) => { arr.forEach((item, index, array) => { if(!result.includes(item)) { // Or result. indexOf (item) ==== -1 result.push(item); } }); return result; }
It is recommended to use include
** Map data structure, do not understand the self-resolution of Map, Portal **
const unique = arr => { const map = new Map(); return arr.filter((item) => !map.has(item) && map.set(item, 1)); }
Object-relational mapping can set different types of keys so that it can quickly collect different data in arr
** Set data structure, no duplicate data is allowed, and Set supports deconstruction Portal **
const unique = arr => Array.from(new Set(arr)); // Or deconstruct it through ES6... const unique = arr => [...new Set(arr)];
Simple and crude
** reduce, given the initial value, gives the final value according to the array loop **
const unique = (arr, result = []) => arr.reduce((prev,curr) => prev.includes(curr) ? prev : [...prev, curr], result);
Three, summary
The method has been said almost, depending on how you use it, there are some similar methods, but gave a description, did not give specific examples, I hope you try it yourself, farewell!