1.indexOf method de duplication 1
The indexOf() method of the array returns the first occurrence of a specified element in the array. The method first defines an empty array res, then calls the indexOf method to traverse the original array. If the element is not in res, its push is entered into res. Finally, the res is returned to get the de weighted array.
function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } let res = [] for (let i = 0; i < arr.length; i++) { if (res.indexOf(arr[i]) === -1) { res.push(arr[i]) } } return res }
2.indexOf method de duplication 2
Use indexOf to detect whether the position of the first occurrence of an element in the array is equal to the current position of the element. If it is not equal, it indicates that the element is a duplicate element
function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } return Array.prototype.filter.call(arr, function(item, index){ return arr.indexOf(item) === index; }); }
3. Double cycle weight removal
Double for (or while) loop is a clumsy method. Its implementation principle is very simple: first define an array containing the first element of the original array, then traverse the original array, compare each element in the original array with each element in the new array, if it is not repeated, add it to the new array, and finally return the new array; Because its time complexity is O(n^2), if the array length is large, it will consume a lot of memory
function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } let res = [arr[0]] for (let i = 1; i < arr.length; i++) { let flag = true for (let j = 0; j < res.length; j++) { if (arr[i] === res[j]) { flag = false; break } } if (flag) { res.push(arr[i]) } } return res }
4. De duplication of adjacent elements
This method first calls the sorting method sort() of the array, and then traverses and compares adjacent elements according to the sorted results. If they are equal, the modified elements will be skipped until the traversal is completed
function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } arr = arr.sort() let res = [] for (let i = 0; i < arr.length; i++) { if (arr[i] !== arr[i-1]) { res.push(arr[i]) } } return res }
5.set and deconstruction assignment de duplication
The data type set is added in ES6. One of the biggest features of set is that the data is not repeated. The set function can accept an array (or array like object) as a parameter to initialize. This feature can also be used to de duplicate the array
function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } return [...new Set(arr)] }
6. Use object attributes to remove duplicates
Create an empty object, traverse the array, set the value in the array as the attribute of the object, and assign the initial value of 1 to the attribute. Each time it occurs, the corresponding attribute value increases by 1. In this way, the attribute value corresponds to the number of occurrences of the element
function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } let res = [], obj = {} for (let i = 0; i < arr.length; i++) { if (!obj[arr[i]]) { res.push(arr[i]) obj[arr[i]] = 1 } else { obj[arr[i]]++ } } return res }
7.Array.from and set de duplication
The Array.from method can convert the set structure into an array result, and we know that the set result is a non duplicate data set, so we can achieve the purpose of de duplication
function unique(arr) { if (!Array.isArray(arr)) { console.log('type error!') return } return Array.from(new Set(arr)) }