Home > Web Front-end > JS Tutorial > Removes duplicate entries from an array (there may be multiple entries), and the return value is a new array containing only the deleted duplicate entries. _javascript skills

Removes duplicate entries from an array (there may be multiple entries), and the return value is a new array containing only the deleted duplicate entries. _javascript skills

WBOY
Release: 2016-05-16 19:08:36
Original
1060 people have browsed it

Today I saw Taobao's online recruitment on bluediea. I believed that Taobao's technology is still very good, so I quickly opened it, took a look at the questions and tried to do it.
When I got to the third question, I was stuck for a long time. It wasn’t that I couldn’t do it. I felt that there was a very simple method but I couldn’t think of it at the moment. However, after a lot of effort, I finally completed this question and gave the answer. An answer that I am quite satisfied with, welcome your suggestions!

The question is as follows:
Please add a prototype method to the Array local object. Its purpose is to delete duplicate entries (there may be multiple) in the array entries. The return value is a value that only contains the deleted duplicates. A new array of entries.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]
The code is 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])
                                                                                                       ;
this.splice(i-1, 1); Prepare this[ i-1] is stored in the variable val to reduce the number of accesses.
2. Use the array.push method to replace the method of using variables to save the length of the array.

Very good! Like it!
I also implemented the filtering of returned duplicate items, which may be due to a misunderstanding of the meaning of the question.
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template