JavaScript deletes duplicates in an array (uniq) _javascript tips

Source: Internet
Author: User
Code that can be used directly: the cloud-dwelling Community revised edition
<script> function Unique (data) {data = Data | | []; var a = {}; len = data.length; for (var i=0 i<len;i++) {var v = data[i]; if (typeof (A[v]) = = ' undefined ') {a[v] = 1; } }; data.length=0; for (var i in a) {data[data.length] = i; } return data; function test () {var arr = [9,1,3,8,7,7,6,6,5,7,8,8,7,4,3,1]; var arr1 = unique (arr); Alert (Arr1.join (",")); Test (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Here is the advanced tutorials and instructions, like with in-depth friends can refer to the next.
First of all, let's look at the YUI is how to deal with:
Copy Code code as follows:

var toobject = function (a) {
var o = {};
for (var i = 0; i < a.length i = i+1) {
O[a[i]] = true;
}
return o;
};

var keys = function (o) {
var a=[], I;
For (i in O) {
if (Lang.hasownproperty (o, i)) {//Yui's method
A.push (i);
}
}
return A;
};

var uniq = function (a) {
return keys (Toobject (a));
};

Detailed analysis, see colleagues in the days of sharing "cleverly remove the duplicates in the array."

The way you use is very similar to YUI's way, but only use a loop to complete the deletion of duplicates in the array, as follows:
Copy Code code as follows:

var uniq = function (arr) {
var a = [],
o = {},
I
V
len = arr.length;

if (Len < 2) {
return arr;
}

for (i = 0; i < len; i++) {
v = arr[i];
if (O[v]!== 1) {
A.push (v);
O[V] = 1;
}
}

return A;
}

After a simple test: their use of the way the performance is much higher than the YUI way.

We are welcome to provide a better way to deal with this.

December 28, 2009 Update:

Neither of these function methods can temporarily handle complex arrays of mixed types (thanks to kittens ' questions), such as: [0, "0", 1, "1", 0], ["null", NULL].

We can use the improved function method (thanks to the Closurecache) for the ability to contract the type as a number (note: A number that requires a non-0 start) or an array of strings:
Copy Code code as follows:

var uniq = function (arr) {
var a = [],
o = {},
I
V
CV,//corrected value
len = arr.length;

if (Len < 2) {
return arr;
}

for (i = 0; i < len; i++) {
v = arr[i];

/* Closurecache is using CV = v + 0 in the function provided by
* This will not be able to distinguish a similar [1, 10, "1", "10"] array,
* Because after the Operation => 1, 10, 10, 100, it is obvious that a duplicate identifier appears.
* Is there any problem in front of the plus?
* Some: The array can not appear similar to 01, 001, starting with 0 digits,
* But the applicability is wider than previously.
*/
CV = 0 + V;

if (!O[CV]) {
A.push (v);
O[CV] = true;
}
}

return A;
}

If you want to solve the problem on the basis of ideas, a little more perfect, recommend Dexter.yy method, type judgment, give a unique indicator, see comments on the 20 floor.

There is no best, only the most appropriate way, in fact, using Array.indexof () is also a good idea to choose, for the browser has been supported directly with the native Array.indexof () method, for unsupported, we add Array.indexof () method, as follows:
Copy Code code as follows:

if (! ARRAY.PROTOTYPE.INDEXOF) {
Array.prototype.indexOf = function (obj, fromindex) {
if (Fromindex = = null) {
Fromindex = 0;
else if (Fromindex < 0) {
Fromindex = Math.max (0, this.length + fromindex);
}

for (var i = Fromindex i < this.length; i++) {
if (this[i] = = obj)
return i;
}
return-1;
};
}

Next, the process of implementation is very simple.

For optimization tips for implementing a scenario using the Array.indexof () method: When the same value is found, remove from the array to reduce the amount of the next traversal.

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.