Remove duplicate javascript Array

Source: Internet
Author: User
Tags return tag javascript array

In general, the interview process has gained a lot. The main reason is to identify the gap between yourself and the narrow knowledge. It is necessary to crack down on self-confidence. Here is a comprehensive summary about the de-duplication of javascript arrays.

Consider a problem that is relatively easy to accept from simplicity to complexity. First of all, it is assumed that the array to be de-duplicated is relatively simple, for example:
Copy codeThe Code is as follows:
Var arr = [1, 2, 3, '5', 6, 5, '','']

This array only contains numbers and strings. We add the deduplication method distinct to the array prototype, which is implemented using the first method that is easy to think of. Of course it is also very stupid and straightforward, copy this array and then loop through the two arrays to compare whether the current value is equal to all values after it. If it is not equal to all values after it, save the value to the new array, then the new array is returned. The method is as follows:
Copy codeThe Code is as follows:
// Method 1
Array. prototype. distinct = function (){
Var clone, newArr = [], n = 0;
If (this. length <2) return;
Clone = this;
For (var I = 0, len = this. length; I <len; I ++ ){
For (var j = I + 1, len2 = clone. length; j <len2; j ++ ){
If (this [I]! = Clone [j]) {
N ++;
}
}
If (n = (len-i-1 )){
NewArr. push (this [I])
}
N = 0;
}
Return newArr;
}
Console. log ([1, 2, 3, '5', 6, 5, '','']. distinct ());
/* Obtain the radio value to be checked */
Function GetRadioValue (RadioName ){
Var obj;
Obj = document. getElementsByName (RadioName );
If (obj! = Null ){
Var I;
For (I = 0; I <obj. length; I ++ ){
If (obj [I]. checked ){
Return obj [I]. value;
}
}
}
Return null;
}

/* Set the selected Attribute */
Function SetRadioCheck (RadioName, I ){
Var obj;
Obj = document. getElementsByName (RadioName );
Obj [I]. setAttribute ("checked", "checked ");
}

Basically, it can meet our needs. For such a simple type, we do not need to spend too much time, but what if the array is very long? In this way, the length of the array is n, and the time complexity is n * n. Obviously, the performance of this method remains to be improved. The second method is to use array sorting to remove duplicate values during sorting.
Copy codeThe Code is as follows:
// Method 2
Array. prototype. distinct = function (){
Var newArr = this. concat (). sort (), self = this;
NewArr. sort (function (a, B ){
Var n;
If (a = B ){
N = self. indexOf ();
Self. splice (n, 1 );
}
});
Return self;
}
Console. log ([, '5', 6, 6, 6, 15, 5, '5', 5, '','']. distinct ());

In this way, the Code seems to be much shorter, even without a for loop, but the sort efficiency is not high. Let's take a look at the third implementation method. The object attributes used will not be renamed.
Copy codeThe Code is as follows:
// Method 3
Array. prototype. distinct = function (){
Var newArr = [], obj = {};
For (var I = 0, len = this. length; I <len; I ++ ){
If (! Obj [this [I]) {
NewArr. push (this [I]);
Obj [this [I] = 'new ';
}
}
Return newArr;
}
Console. log ([, '5', 6, 6, 6, 15, 5, '5', 5, '','']. distinct ());

If you run the third method and check the result, you will find that the result is inconsistent with the implementation result of the preceding method. For details, you can see that the number 5 and string 5 are removed as repeated values. It seems that the type must be saved before determining whether it is equal, so that the supplementary version of the third method below is available.
Copy codeThe Code is as follows:
// Method 3 supplementary Edition
Array. prototype. distinct = function (){
Var newArr = [], obj = {};
For (var I = 0, len = this. length; I <len; I ++ ){
If (! Obj [typeof (this [I]) + this [I]) {
NewArr. push (this [I]);
Obj [typeof (this [I]) + this [I] = 'new ';
}
}
Return newArr;
}

The example above is a simple type. Let's test it with a more complex type.
Copy codeThe Code is as follows:
Console. log ([1, null, 2, {a: 'vc '}, {}, '5', 6,5, 6, {a: 'vv, '5', 5, '','', [1], [1], [1, 2],]. distinct ());

We found that the different objects {a: 'vc '}, {}, {a: 'vv'} will still be removed, if an object exists in the array, You need to traverse the attributes and values in the object and continue the enhancement of the third method.
Copy codeThe Code is as follows:
// Method 3 enhanced Edition
Array. prototype. distinct = function (){
Var sameObj = function (a, B ){
Var tag = true;
If (! A |! B) return false;
For (var x in ){
If (! B [x])
Return false;
If (typeof (a [x]) === 'object '){
Tag = sameObj (a [x], B [x]);
} Else {
If (a [x]! = B [x])
Return false;
}
}
Return tag;
}
Var newArr = [], obj = {};
For (var I = 0, len = this. length; I <len; I ++ ){
If (! SameObj (obj [typeof (this [I]) + this [I], this [I]) {
NewArr. push (this [I]);
Obj [typeof (this [I]) + this [I] = this [I];
}
}
Return newArr;
}

The above example shows that there is a problem with the basic wood. Of course, the test can be more abnormal and more entangled. I will not go into it here. At present, this method is quite complete on the Internet, if you have a better and more perfect method, please do not hesitate to give us some advice.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.