This is the third day of my participation in Gwen Challenge
Relearn JavaScript in a series of articles…
Scene:
There is an array: [1,2,1,2,3,4,3]. The algorithm is used to duplicate the elements in the array, and the final array is [1,2,3,4].
1. Go through the number group
Create an array inside the function and iterate over the array passed in. If the iterated value is not in the new array, it is added to it, otherwise it is not processed.
function unique(array){ let result = [] for(let i=0; i<array.length; I++) {if (result. IndexOf (array [I]) = = = 1) {/ / numerical result. No new array push (array) [I]}} return result} unique / / (,2,1,2 [1]) [1, 2]Copy the code
2. Use object key-value pairs
Create a new js object and a new array. Check whether the current value is the key of the js object. If yes, the element already exists and no action is taken. If the element is present for the first time, insert the key into the js object and insert the new array.
function unique(arrays){ let obj = {}, result = [], val, type; for(let i=0; i<arrays.length; I ++){val = arrays[I] // The same Number and String keys return the same values. obj[val]){ obj[val] = [type] result.push(val) }else if(obj[val].indexOf(type)===-1){ // obj[val].push(type) Result. The push return result (val)}}} unique ([' 1 ', 1,1,2,3,2,3]) / / [' 1 ', 1, 2, 3]Copy the code
3. Sort first, then undo
Sort (); sort(); sort(); sort();
function unique(arrays){ let result = [arrays[0]] arrays.sort((a,b)=>a-b) for(let i=0; i<arrays.length; i++){ if(arrays[i]! = = the result [result. The length - 1]) {result. Push (arrays [I])}} return result} unique (,4,5,7,4 [1]) / /,4,5,7 [1]Copy the code
4. Iterate the number group first
We specify the index I and j of the loop respectively. The initial value of j is I +1. In each layer of the loop, compare whether the values of indexes I and J are equal. If the values are equal, it means that the same values appear in the array. Therefore, update indexes I and J, and then compare the values of the new indexes. At the end of the loop, we get an index value, I, that doesn’t have the same value to the right of, and we push it into the result array.
function unique(arrays){ let result = [] for(let i=0,l=arrays.length; i<arrays.length; i++){ for(let j=i+1; j<l; j++){ if(arrays[i]===arrays[j]){ j = ++i } } result.push(arrays[i]) } return result }Copy the code
5. Based on reduce() function
Similar to algorithm 2, use a key-value object to check whether the key is repeated in the reduce() function loop.
function unique(arrays){ let obj = {},type; return arrays.reduce(function(preVal,curVal){ type = typeof curVal if(! obj[curVal]){ obj[curVal] = [type] preVal.push(curVal) }else if(obj[curVal].indexOf(type)<0){ obj[curVal].push(type) PreVal. Push (curVal)} return preVal}, [])} unique (/,2,3,1,2,3 1, '1') / / [1, 2, 3, "1"]Copy the code
6. Use the Set data structure of ES6
A Set is a data structure similar to an array, except that its members are unique.
function unique(arrays){
return Array.from(new Set(arrays))
}
Copy the code
7. Use the Map data structure of ES6
Map is a structure that stores data based on key-value. Each key can only correspond to a unique value. In addition, the Map key identifies data of different types. That is, 1 and “1” are treated as different keys in the Map.
function unique(arrays){ let map = new Map() return arrays.filter(item=> ! map.has(item) && map.set(item,1) ) }Copy the code
8. To summarize
So far we have summarized seven algorithms for array de-duplication.