JavaScript removes repeating elements from an array _javascript tips

Source: Internet
Author: User
In the process of writing a program, you often encounter the need to remove duplicate elements from an array. It is not difficult to implement this function.
We can do this with a double loop, and for small arrays, that's certainly not the case.
But if our arrays are larger, there are tens of thousands of elements inside. Then, with double loops, efficiency is extremely low.
Here we will use the characteristics of JS, write an efficient way to remove the array of duplicate elements.
Copy Code code as follows:

<script>
function unique (data) {
data = Data | | [];
var a = {};
for (var i=0; i<data.length; 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>

Output results:
9,1,3,8,7,6,5,4
JS Array to the weight of the array is to remove the repeated elements:
Copy Code code as follows:

Array.prototype.delrepeat=function () {
var newarray=new Array ();
var len=this.length;
for (var I=0;i<len; i++) {
for (Var j=i+1;j<len;j++) {
if (This[i]===this[j]) {
J=++i;
}
}
Newarray.push (This[i]);
}
return newarray;
}

But it's clear there's a for loop with another for loop in it, and it's going to be time-consuming in large amounts of data! Low efficiency! After searching and pointing, a new approach has been optimized:
Copy Code code as follows:

Array.prototype.delrepeat=function () {
var newarray=[];
var provisionaltable = {};
for (var i = 0, item; (item= this[i])!= null; i++) {
if (!provisionaltable[item]) {
Newarray.push (item);
Provisionaltable[item] = true;
}
}
return newarray;
}

is to use a temporary provisionaltable object, the value of the array as the key value of the Provisionaltable object, and push the value of the array into the new array if the corresponding value does not exist.
The efficiency is improved, but there is a bug in the assumption that the array is swapped with convertible numbers and strings, such as the array [6, "6"] which is a good time to get rid of one. Tragedy, while seeking solutions.

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.