Method One (recommended)
1. Create an empty object
2. Create a new array to hold the result
3.for Loop, each time an element is taken out and compared to the object, if the element is not duplicated, it is stored in the result array, and the content of the element as an object property, and assigned a value of 1, deposited in the 2nd step of the object created.
Note: As for how to compare, it is to remove an element from the original array, and then go to the object to access the property, if you can access the value, then the description is repeated.
function Unique (arr) {var obj = {};var New_arr = [];for (var i =0;i<arr.length;i++) {if (!obj[arr[i]]) {New_arr.push (arr[ I]); Obj[arr[i]] = 1;}} return New_arr;} var arr1 = [1,1,33,4,6,9,12,12,14];var arr = unique (arr1); alert (arr); 1,33,4,6,9,14
Method Two: The most conventional
1. Build a new array to store the results
Each time an element is removed from the original array in a 2.for loop, the loop is compared with the result array.
3. If the element is not in the result array, it is stored in the result array
function Unique (arr) {for (var i =0;i<arr.length-1;i++) {for (Var j=i+1;j<arr.length;j++) {if (arr[i]== Arr[j]) { Arr.splice (j,1); j--;}} return arr;} var arr1 = [1,1,33,4,6,9,12,12,14];var arr = unique (arr1); alert (arr);
Method Three
1. Sort the original array first
2. Check that the first element in the original array is the same as the last element in the result array, because it is already sorted, so the repeating element is in the adjacent position
3. If it is not the same, the element is stored in the result array
JavaScript array de-weight