Deletes a duplicate entry in an array entry (there may be multiple), and the return value is a new array that contains only duplicate entries that were deleted. _javascript Tips

Source: Internet
Author: User
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.

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.