General Introduction
The array of JavaScript to go to the weight of the problem in many interviews will be met, now make a summary
To build an array first
var arr = [1,2,3,3,2,'我','我',34,'我的',NaN,NaN];
First Kind
Train of thought: Create a temporary array, use for loop to determine whether each item in ARR has the same value in the temporary array, if not, add the value to the temporary array, if there is the same value, and then return the temporary array
Code:
Array.prototype.removeDuplicate = function () {
var n = [];
for (Var i=0;i<this.length;i++) {
if (N.indexof (this[i)) = = 1) {
N.push (this[i]);
}
return n;
}
var m = arr.removeduplicate ();
Console.log (m);//[1, 2, 3, "I", 34, "my", Nan, Nan]
Note: Duplicate Nan values are not removed
Second Kind
Idea: Create a temporary array, use the IndexOf () method with the For loop to sequentially determine where each item in the ARR first appears in the ARR, if the first occurrence of the item in the ARR is its position, indicating that there is no equivalent value before it is added to the temporary array. If the first occurrence of the item in Arr is not his position, it indicates that it has the same value before it, and does not add it to the temporary array; the last return of the temporary array
Code:
Array.prototype.removeDuplicate = function () {
var n = [];
for (Var i=0;i<this.length-1;i++) {
if (This.indexof (this[i)) = = i) {
n.push (this[i])
;
}
return n;
}
var m = arr.removeduplicate ();
Console.log (m);//[1, 2, 3, "I", 34, "my"]
Note: The Nan value will be deleted
Third Kind
Idea: Create a temporary object, use the For loop to detect if this temporary object has arr[i] This property, if there is no this property indicates that Arr[i has no value that it duplicates before it. Set the Arr[i property of the temporary object to true, indicating that it has this property and adds the item to the temporary array; Last return the temporary array
Code:
Array.prototype.removeDuplicate = function () {
var n = [],m = {};
for (Var i=0;i<this.length;i++) {
if (!m[this[i]]) {
M[this[i]] = true;
N.push (This[i]);
}
return n;
}
var m = arr.removeduplicate ();
Console.log (m);//[1, 2, 3, "I", 34, "my", NaN]
Fourth Kind
Idea: Sort the array first, then compare the values of each item and the item following it, and add it to the temporary array if it is not equal;
Code:
Array.prototype.removeDuplicate = function () {
var n = [];
This.sort ();
for (Var i=0;i<this.length;i++) {
if (This[i]!= this[i+1]) {
N.push (this[i));
}
return n;
}
var m = arr.removeduplicate ();
Console.log (m);//[1, 2, 3, Nan, Nan, "I", "my"]
Note: Duplicate Nan values are not removed
Fifth Kind
Idea: Use ES6 method Set method to go heavy, and transform the array with Array.from
A set is a new data structure that can receive an array or a class array object, automatically repeating items in it, and returning an object
Code:
Array.prototype.removeDuplicate = function () {return
(Array.from (The new Set (this));
}
var m = arr.removeduplicate ();
Console.log (m);//[1, 2, 3, "I", 34, "my", NaN]
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!