-
Creates a new empty array, iterates through For...of (ES6), and, by IndexOf, determines whether the element exists in the new array, and then push the nonexistent (INDEXOF (n) ==-1) element to the new array:
let arr = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2]; function Removedup_indexof (Originalarr) {Let newArr = []; for (n of Originalarr) {if (Newarr.indexof (n) ==-1) {Newarr.push (n); }} return NEWARR; } Let arr1 = Removedup_indexof (arr);
The can also first place the first item of the original array in a new array, starting with the second entry to determine the loop:
Let arr = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2]; function Removedup_index (Originalarr) {//index= 0 of the items are added to the array first, then from the second start loop let NEWARR = [originalarr[0]]; for (Var i=1;i<originalarr.length;i++) {if (Originalarr.indexof (Originalarr[i]) ==i) {Newarr.push (origin Alarr[i]); }} return NEWARR; } Let arr2 = Removedup_index (arr);
- The
-
uses the comparison of adjacent arrays to push the elements that are different from the previous element into the array, NOTE : This way, the array is sorted first, and the original array is sorted deep with the slice () method to avoid affecting the original array.
Let arr = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2];function compareadjoin (Originalarr) {let ordered ARR = Originalarr.slice (). sort ();//sorted first, then adjacent array elements are compared let NEWARR = [orderedarr[0]]; for (Var i=1;i<orderedarr.length;i++) {if (orderedarr[i]!=orderedarr[i-1]) {Newarr.push (ordered Arr[i]); }} return NEWARR; } Let Arr3 = Compareadjoin (arr);
-
Takes advantage of Object{key:value}, which is an attribute that cannot be duplicated by a key value. Create a new obj{}, add each element of the array as a key to obj, and push the element that does not exist obj[arr[i]] to the new array, obj= {arr[1]: Value,arr[2]:value ...}
Note: There is no need to pay attention to value values, can be arbitrarily copied, only by key to determine whether the element has been added.
Let arr = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2], function removedup_obj (Originalarr) {Let Newa Rr=[]; Let obj={}; for (n of Originalarr) {if (!obj[n]) {obj[n] = 1; Newarr.push (n); }} return NEWARR; } Let ARR4 = Removedup_obj (arr);
The flashback loop (to avoid the change in the length of the array in the deletion process) ordered array, will be the same as the next element through the splice method removed from the original array, with the same #, the array first sorted, with the slice () method deep copy the original array to order, to avoid affecting the original array.
let arr = [1,1,2,2,3,6,3,77,88,5,98,9,7,2,7,5,3,4,2]; function removeDup_splice(originalArr){ let orderedArr = originalArr.slice().sort(); for(var i = orderedArr.length-1;i>0;i--){ if(orderedArr[i-1]===orderedArr[i]){ orderedArr.splice([i-1],1); } } return orderedArr; } let arr5 = removeDup_splice(arr);