JavaScript arrays Remove repetitive _javascript tips

Source: Internet
Author: User
Tags return tag javascript array
Generally speaking, the process of the interview or harvest a lot, is mainly to understand their differences in the end how much, the extent of knowledge in the end how narrow, the appropriate blow to self-confidence or necessary. Here's a comprehensive summary of the JavaScript array to weight the problem.

Consider a problem from simple to complex relatively easy to accept a point, first of all assume that to go to the heavy array is relatively simple, for example:
Copy Code code as follows:

var arr=[1,2,2,3, ' 5 ', 6, 5, ', ']

This array contains only numbers, two types of strings. We add a distinct method to the array prototype. With the first very easy to think of the method to achieve, of course, is also very straightforward, copy the array and then loop two of the array, compare the current value and all subsequent values are equal, if not all of the following values will be stored in the new array, So the last return of the new array. The method is as follows:
Copy Code code as follows:

The first of these methods
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,2,3, ' 5 ', 6,5, ', ', '].distinct ());
/* To obtain the value of the check radio * *
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;
}

/* settings are selected Properties * *
function Setradiocheck (radioname,i) {
var obj;
Obj=document.getelementsbyname (Radioname);
Obj[i].setattribute ("Checked", "checked");
}

Basic to meet our needs, for such a simple type comparison does not cost too much brain, but if the array is very long? So traversing the array, the length of the array is n, then the time complexity is n*n. Obviously, the performance of this method still needs to be improved. Next is the second method, which uses array sorting to remove duplicate values in the sort process.
Copy Code code as follows:

The second method
Array.prototype.distinct=function () {
var newarr=this.concat (). Sort (), self=this;
Newarr.sort (function (a,b) {
var n;
if (a===b) {
N=self.indexof (a);
Self.splice (n,1);
}
});
return self;
}
Console.log ([1,2,2,3, ' 5 ', 6,5,6,6,15,5, ' 5 ', 5, ', ', '].distinct ());

The code looks a lot shorter and doesn't even have a for loop, but the sort is less efficient. Let's take a look at the third implementation method, the use of the object attribute does not duplicate the principle
Copy Code code as follows:

The third method
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 ([1,2,2,3, ' 5 ', 6,5,6,6,15,5, ' 5 ', 5, ', ', '].distinct ());

The third way to run the look at the results, you will find that the results of the implementation of the above inconsistent, looked at the original number 5 and string 5 as a duplicate value to remove. It appears that the type must be saved and then judged to be equal, so that there is a supplemental version of the third method below
Copy Code code as follows:

Third Method Supplement
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 very simple type, and we test it with a more complex type.
Copy Code code as follows:

Console.log ([1,null,2,{a: ' VC '},{}, ' 5 ', 6,5,6,{a: ' VV '},15,5, ' 5 ', 5, ', ', ', ' [1],[1],[1,2],,].distinct ()];

Found {a: ' VC '},{},{a: ' VV '} These different objects will be removed, if there are objects inside the array to continue to traverse the object inside the attributes and values, continue to strengthen the third method
Copy Code code as follows:

The third way to strengthen the version of
Array.prototype.distinct=function () {
var sameobj=function (a,b) {
var tag = true;
if (!a| |! b) return false;
for (Var x in a) {
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;
}

With the above example test found that the basic wood has problems, of course, the test can be more perverted more entangled, here is not to delve into, now see this method in the online belong to a relatively complete, if there is a better and more perfect method please do not hesitate to enlighten.
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.