Summary of deduplication methods for javascript Arrays

Source: Internet
Author: User

Summary of deduplication methods for javascript Arrays

Summary of deduplication methods for javascript Arrays

Array deduplication is a common requirement. We will temporarily consider repeated arrays of the same type. It mainly aims to clarify the ideas and consider the performance. The following methods are available on the Internet. Here is a brief summary.

Summary of deduplication methods for javascript Arrays

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

Array. prototype. unique1 = function (){

Var n = []; // a new temporary Array

For (var I = 0; I <this. length; I ++) // traverses the current array

{

// If the I of the current array has been saved as a temporary array, skip this step,

// Otherwise, push the current item to the temporary array.

If (n. indexOf (this [I]) =-1) n. push (this [I]);

}

Return n;

};

Array. prototype. unique2 = function ()

{

Var n = {}, r = []; // n is a hash table, and r is a temporary array.

For (var I = 0; I <this. length; I ++) // traverses the current array

{

If (! N [this [I]) // If the hash table does not have the current item

{

N [this [I] = true; // save it to the hash table

R. push (this [I]); // pushes the current item of the current array to the temporary array.

}

}

Return r;

};

Array. prototype. unique3 = function ()

{

Var n = [this [0]; // result Array

For (var I = 1; I <this. length; I ++) // traverse from the second entry

{

// If the position where the I entry of the current array appears for the first time in the current array is not I,

// This indicates that the I-th item is repeated and ignored. Otherwise, the result array is saved.

If (this. indexOf (this [I]) = I) n. push (this [I]);

}

Return n;

};

Array. prototype. unique4 = function ()

{

This. sort ();

Var re = [this [0];

For (var I = 1; I <this. length; I ++)

{

If (this [I]! = Re [re. length-1])

{

Re. push (this [I]);

}

}

Return re;

};

Var arr = [1, 2, 2, 3, 4, 5];

Console. log (arr. unique1 (); // [1, 2, 3, 4, 5]

Console. log (arr. unique2 (); // [1, 2, 3, 4, 5]

Console. log (arr. unique3 (); // [1, 2, 3, 4, 5]

Console. log (arr. unique4 (); // [1, 2, 3, 4, 5]

The indexOf method of the array is used in both the 1st and 3rd methods. The purpose of this method is to find the location where the saved parameter appears for the first time in the array. Obviously, the js engine will traverse the array when implementing this method until it finds the target. Therefore, this function will waste a lot of time. The method in 2nd uses the hash table. Store existing objects in the form of a subobject. The reference of the underlying object is much faster than the search for an Array Using indexOf.

The fourth method is to first sort the array and then compare the adjacent values. The JS native sort method is used for sorting. The JS engine should use quick sorting internally. The final test result is that the average running time of this method is about three times that of the second method, but it is much faster than the first and third methods.

The above is all the content of this article. I hope you will like it.

Related Article

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.