Summary of how javascript deletes repeated elements in an array

Source: Internet
Author: User

Summary of how javascript deletes repeated elements in an array

This example describes how to delete repeated elements in an array using javascript. Share it with you for your reference. The specific analysis is as follows:

Here is a high-frequency front-end interview question, mainly used to delete array repeated elements in javascript. Hope to help beginners

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

// Array deduplication Method

Array. prototype. unique = function (){

// Declare variables in a centralized manner

Var

OldArr = this,

NewArr = [oldArr [0],

Len = oldArr. length,

I = 1;

// Filter empty Arrays

If (! Len) return this;

// Filter duplicate elements

For (; I <len; I ++ ){

NewArr. indexOf (oldArr [I]) <0? NewArr. push (_ this ):'';

}

// The returned filtered array does not affect the original array.

Return newArr;

}

Var arr = ['A', 'A', 'B', 'A', 'C', 'D'];

Console. log (arr. unique ());

// ["A", "B", "c", "d", unique: function]

Although there are a lot of data on the Internet, however, after all, the logic you write is clear, you can also follow the logic extension, such as extending to object elements to deduplicate, or you can operate multiple arrays at the same time. Here, you can put the writing methods of others to make a comprehensive comparison. lower

Method 1:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Function oSort (arr)

{

Var result = {};

Var newArr = [];

For (var I = 0; I

{

If (! Result [arr [I])

{

NewArr. push (arr [I])

Result [arr [I] = 1

}

}

Return newArr

}

Method 2:

Traverse the array arr to be deleted and put the elements into the tmp of another array. The element can be put into tmp only when it is determined that the element does not exist in arr.

Two functions are used: for... in and indexOf ()

?

1

2

3

4

5

6

7

8

9

10

11

Var student = ['qiang ', 'ming', 'Tao', 'lil', 'liang', 'you', 'qiang ', 'Tao'];

Function unique (arr ){

// Traverse the arr and put the elements into the tmp array separately (only when the elements do not exist)

Var tmp = new Array ();

For (var I in arr ){

// This element does not exist in tmp before it can be appended.

If (tmp. indexOf (arr [I]) =-1 ){

}

}

Return tmp;

}

Method 3:

Changing the element values and key positions of the target array arr will automatically delete the repeated elements. After the replacement, the array ('qiang '=> 1, 'ming' => 1, 'Tao' => 1)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<Script type = "text/javascript">

Var student = ['qiang ', 'ming', 'Tao', 'lil', 'liang', 'you', 'qiang ', 'Tao'];

Function unique (arr ){

Var tmp = new Array ();

For (var m in arr ){

Tmp [arr [m] = 1;

}

// Switch the key and value positions again

Var tmparr = new Array ();

For (var n in tmp ){

Tmparr. push (n );

}

Return tmparr;

}

</Script>

Method 4

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

* Remove repeated array elements

*/

Function uniqueArray (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 ){

Data [data. length] = I;

}

Return data;

}

Methods are almost the same. The third method is very clever ~

I hope this article will help you design javascript programs.

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.