Today in Bluediea see Taobao online recruitment, I believe the technology is still very cow Taobao, so hurriedly opened to see the topic and try to do.
To the third problem card for a while, it is not do not come out, feel a very simple way but I can not think of a moment, but the Dickens finally completed the subject, gave a more satisfactory answer, welcome friends to guide!
The topics are as follows:
Add a prototype method to the array local object that is used to delete the duplicate entries in the array entry (there may be multiple), and the return value is a new array that contains only duplicate entries that were deleted.
<textarea id="runcode72979"><script type= "Text/javascript" > <!--array.prototype.delrepeat=function () {//temparr: Duplicate item flags, TE MPARR1: Save duplicate array entries var temparr=[],temparr1=[],j=0; Traversal array for (Var i=this.length;i>0;i--) {//If the item has not yet been duplicated if (!temparr[this[i-1]]) { Temparr[this[i-1]]=1; The item is marked by an associative array and placed to 1 (on behalf of one occurrence)}else{if (temparr[this[i-1]]++==1) temparr1[j++]=this[i-1]; If the duplicate is saved for the first time, it is repeated with the flag +1 This.splice (i-1,1); Delete the item from the original array (because it is in reverse traversal, so you can delete it directly)}} return tempArr1; Returns the list of duplicates} var a=new array (5,410,52, ",", 24,5, ",", true,false,24,24,25,5) alert ("Original array:" +a); Alert ("Duplicates:" +a.delrepeat ()); Alert ("Remaining item:" +a); --> </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Copy Code code as follows:
Array.prototype.delrepeat=function ()
{
var tmparr=[],rs=[],i,val;
for (i=this.length;i>0;i--)
{
val = this[i-1];
if (!tmparr[val])
{
Tmparr[val]=1;
}else
{
Rs.push (Val);
This.splice (i-1,1);
}
}
Tmparr = null;
Return RS;
}
Two advantages:
1, Advance this[i-1] in the variable Val, reduce the number of visits.
2, using the Array.push method, replace the use of variables to save the length of the array methods.
Very good! Praise One!
I also conveniently implemented a filter to return duplicates, which may be biased in understanding the question.