1. Traversal Array method
The simplest way to do this is to create a new array, iterate through the incoming array, and add the value to the new array without the new array. Note: The method of determining whether a value is in an array "IndexOf" is a ECMAScript5 method, IE8 is not supported below, Need to write more compatible with the lower version of the browser code, the source code is as follows:
The simplest array goes to the heavy method
function unique1 (array) {
var n = [];//A new temporary array
//traversal of the current array for
(var i = 0; i < array.length i + +) {
//If the current array has already been saved in the temporary array, skip,
//or push the current item to the temporary array if
(N.indexof (array[i]) = = 1) n.push (Array[i]);
} return
N;
2. Object-Key-value pair method
The method executes faster than any other method, that is, to occupy more memory, to realize : A new JS object and a new array, traversing the incoming array, determine whether the value of the JS object key, not the words to add the key to the object and put in the new array. Note: When determining whether a JS object key, will automatically execute the incoming key "toString ()", different keys may be mistaken for the same; for example: A[1], a["1"]. To solve the above problems, we have to call "IndexOf".
Fastest, takes up space (space for 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;
3. Array subscript Judgment method
Still have to call "IndexOf" performance is similar to Method 1, the realization of the idea : If the current array of the first occurrence of the current array position is not I, then that the item I is repeated, ignored. Otherwise, the result array is saved.
function Unique3 (array) {
var n = [array[0]];//result array//
(var i = 1; i < Array.Length; i++) {
//if the first occurrence of the current array in the current array is not I,
//It means that item I is duplicated and ignored. Otherwise deposit the result array
if (Array.indexof (array[i]) = = i) N.push (Array[i]);
}
return n;
4. After sorting the adjacent removal method
Although the "sort" method of the native array results in a less reliable order, this shortcoming has no effect on the sequential weight. implementation Idea : Sort the incoming array, sort the same values next to each other, and then traverse the newer array to add only values that are not duplicates of the previous value.
Adjacent to the same value, and then traversal to remove the duplicate value
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;
}
5. Optimized traversal Array method
The implementation of this method is quite cool, the realization of the idea : Get the right one without repeating the value of the new array. (The next round of judgment that terminates the current loop and goes to the top loop when duplicate values are detected) recommended
Train of thought: get the right one with no duplicates put the 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;
To determine if the browser supports INDEXOF, indexOf for ecmaScript5 new method IE8 the following (including IE8, IE8 only support part ECMA5) is not supported
if (! ARRAY.PROTOTYPE.INDEXOF) {
//new IndexOf method
Array.prototype.indexOf = function (item) {
var result =-1, A_ item = NULL;
if (This.length = = 0) {return result
;
}
for (var i = 0, len = this.length i < len; i++) {
a_item = this[i];
if (A_item = = Item) {Result
= i;
break;
}
}
return result;
}
The above is for you to provide 5 kinds of JS array to the weight of the algorithm to achieve, I hope to help you learn.