Remove duplicate elements of an array

Source: Internet
Author: User

1. Traversal Array method

Idea: Create a new array, iterate over the incoming array value without adding the new array to the new array

Note: Determining whether the value is in the array method IndexOf is ECMAScript5 method, IE8 the following is not supported, requires write compatibility

function unique1 (array) {

var n = []

for (var i = 0;i < array.length;i++) {

if (N.indexof (array[i]) = =-1) {

N.push (Array[i])

}

}

return n;

}

IE8 the following compatible

Determine if the browser supports the IndexOf method

if (! ARRAY.PROTOTYPE.INDEXOF) {

Array.prototype.indexOf = function (item) {

var result =-1

var a_item = null

if (This.length = = 0) {

return result

}

for (var i = 0;i < this.length;i++) {

A_item = This[i]

if (A_item = = = Item) {

result = I

Break

}

}

return result

}

}

2. Object Key-value pair method

Idea: New to JS object and new array, when traversing the incoming array, judge whether the value is the key of the JS object, not to add the key to the object and put in the new array

Note: When judging whether the JS object key, the key will automatically execute ToString (), different keys may be mistaken, for example: a[1], a["1"]

function Unique2 (array) {

var n = {},r = [],val,type;

for (var i = 0;i < array.length;i++) {

val = array[i];

Type = typeof Val;

if (!n[val]) {

N[val]=[type]

R.push (Val)

}else if (N[val].indexof (type) < 0) {

N[val].push (Type)

R.push (Val)

}

}

return R;

}

3. Array subscript Judgment method

Idea: If the current array of item I is in the current array the first occurrence of the position is not I, then the term I is repeated, ignored, otherwise deposited in the result array

function Unique3 (array) {

var n = [Array[0]]

for (var i = 1;i < array.length;i++) {

if (Array.indexof (array[i]) = = i) {

N.push (Array[i])

}

}

return n;

}

4. Post-sorting adjacent removal method

Idea: Sort the incoming array, sort the same values next to each other, and then iterate through the array to add only values that do not duplicate the previous value

function Unique4 (array) {

Array.Sort ()

var re = [Array[0]]

for (var i = 0;i < array.length;i++) {

if (Array[i]!== re[re.length-1]) {

Re.push (Array[i])

}

}

return re

}

5. Optimized traversal Array method

Idea: Get the right-most value that is not duplicated into a new array, to terminate the current loop when the right duplicate value is detected, and to enter the next round of judgment in the top loop

function Unique5 (array) {

var r = []

for (var i = 0;i < array.length;i++) {

for (var j = i + 1;j < array.length;j++) {

if (array[i] = = = Array[j]) {

j = ++i

}

}

R.push (Array[i])

}

return R;

}

Remove duplicate elements of an array

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.