Implementation ideas: Create a new array, iterate through the incoming array, the value is not added to the new array in the new array; Note: The method of judging whether the value is in the array "IndexOf" is the ECMAScript5 method, IE8 the following is not supported, you need to write some compatible low-version browser code:
The simplest array de-weight method
function unique1 (array) {
var n = []; A new temporary array
Iterate through the current array
for (var i = 0; i < Array.Length; i++) {
If the current array of I has been saved into a temporary array, then skip,
Otherwise push the current item to the temporary array
if (N.indexof (array[i]) = =-1) n.push (Array[i]);
}
return n;
}
Object Key-value pair method
Implementation of the idea: Create a new JS object and an array, iterate through the array, determine whether the value is the key of the JS object, not to add the key to the object and put into the new array. Note: When judging whether the JS object key, the incoming key will be automatically executed "toString ()", different keys may be mistaken, for example: a[1], a["1"]. To solve the above problem, you have to call "IndexOf".
Fastest, occupying the most space (space change time)
function Unique2 (array) {
var n = {}, R = [], Len = Array.Length, Val, type;
for (var i = 0; i < Array.Length; i++) {
val = array[i];
Type = typeof Val;
if (!n[val]) {
N[val] = [Type];
R.push (Val);
} else if (N[val].indexof (type) < 0) {
N[val].push (type);
R.push (Val);
}
}
return R;
}
The judgment method of array subscript
Implementation idea: If the current array of item I is in the current array the first occurrence of the position is not I, then the term I is repeated, ignored. Otherwise, the result array is deposited.
function Unique3 (array) {
var n = [array[0]]; Result array
Start traversing from the second item
for (var i = 1; i < Array.Length; i++) {
If the item I of the current array first appears in the current array, the position is not I,
Then it means that item I is repeated and ignored. Otherwise, the result array is stored
if (Array.indexof (array[i]) = = i) N.push (Array[i]);
}
return n;
}
Post-sorting adjacent removal method
Implementation ideas: Sort incoming arrays, sort the same values next to each other, and then iterate through the array to add only values that do not duplicate the previous value.
Next to the same value, and then traverse to remove duplicate values
function Unique4 (array) {
Array.Sort ();
var re=[array[0]];
for (var i = 1; i < Array.Length; i++) {
if (Array[i]!== re[re.length-1])
{
Re.push (Array[i]);
}
}
return re;
}
Optimized traversal Array method
Implementation idea: Gets the right-most value that is not duplicated into the new array. (The next round of judgment that terminates the current loop and enters the top loop when a duplicate value is detected)
Idea: Get the right-most value without duplicates into a new array
function Unique5 (array) {
var r = [];
for (var i = 0, L = array.length; i < L; i++) {
for (var j = i + 1; j < L; j + +)
if (array[i] = = = Array[j]) j = ++i;
R.push (Array[i]);
}
return R;
}