Three methods and code examples for removing the deduplication of JavaScript Arrays

Source: Internet
Author: User

Three methods and code examples for removing the deduplication of JavaScript Arrays

This article mainly introduces three methods and code examples for de-duplicating JavaScript arrays. This article provides the instance Code directly. For more information, see

There are many methods to deduplicate arrays. it is unclear which one is the best. So I tested the effect and performance of array deduplication. Test 100,000 pieces of data. The code and time consumed are as follows.

Which method is used depends on the actual situation.

?

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

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

/* Method 1: 1, '1' will be considered to be the same; all hash objects, such as: {x; 1}, {y: 1} will be considered the same // 10 ms */

Array. prototype. unique = function (){

Var newArr = [], obj = {};

For (var I = 0, len = this. length; I <len; I ++ ){

If (! Obj [this [I]) {

NewArr. push (this [I]);

Obj [this [I] = true;

}

}

Return newArr;

}

 

/* Method 1 ultimate version: All hash objects, such as: {x; 1}, {y: 1}, will be considered the same // 30 ms */

Array. prototype. unique = function (){

Var newArr = [], obj = {};

For (var I = 0, len = this. length; I <len; I ++ ){

If (! Obj [typeof (this [I]) + this [I]) {

NewArr. push (this [I]);

Obj [typeof (this [I]) + this [I] = this [I];

}

}

Return newArr;

}

 

/* Method 2: deduplication results are the best, but performance consumption // 250 ms */

Array. prototype. unique = function (){

Var newArr = this. concat ();

For (var I = 0, len = newArr. length; I <len; I ++ ){

For (var j = I + 1, len = newArr. length; j <len; j ++ ){

// Note ====

If (newArr [I] === newArr [j]) {

NewArr. splice (j, 1 );

J --;

}

}

}

Return newArr;

}

 

/* Method 3: The hash object cannot be deduplicated. // 25 ms */

Array. prototype. unique = function (){

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

For (var I = 0, len = this. length; I <len; I ++ ){

If (newArr. indexOf (this [I]) =-1) {// If the I of the current array has been saved in a temporary array, skip this step. Otherwise, push the current item to the temporary array.

NewArr. push (this [I]);

}

}

Return newArr;

}

 

 

Var arr0 = [11,21, 221,13, 24, "134", "1", {x: 1, y: 1}, {name: "pobaby", age: "12", holobby: "football" },{ name: "pobaby1", age: "121", holobby: "football1" },{ x: 134 },{ y: 132 },{ x: 143 },{ y: 3421}, "mysterious character", "matcher skill combat", "Supersonic Battlefield", "Xiao Xin hitting bricks ", "matcher skill combat", "Garfield cat Superman", "Xiao Xin strike bricks", "mean me 2", "Current Wire", "Apsara trolley ", "Shen D secret character", "matchman S skill combat", "supersound SD speed Battlefield", "Small SD hitting bricks", "matcher SD skill fighting ", "garfi S cat Superman", "Little DF Xin strike bricks", "mean fs I 2", "Electric D flow conductor", "Apsara SD trolley ", "mysterious SD character", "matchgirl skill D skillful combat", "super sound ASD speedy Battlefield", "Xiao Xin beat SAD bricks", "matchgirl skill SD skillful combat ", "Garfield FDS cat Superman", "Xiao Xin hits the SDF brick", "mean SDF me 2", "Current SDF wire", "Apsara hand DF stroller ", "mysterious SD characters", "matchwood talents AS skillful combat", "Supersonic warfare FS Field", "xiaoxin SDF hitting bricks", "matchwood SDF fighting skills ", "Garfield SD cat Superman", 113,231,222 1334, 132, "", "21", {x: 13, y:}, {name: "pobaby2", age: "122", holobby: "football2" },{ name: "pobaby13", age: "1231", holobby: "football41" },{ x: 13544 },{ y: 1352 },{ x: 14543 },{ y: 34521}, "mysterious person sd things", "matcher skill sd skillful combat", "Supersonic sd Battlefield ", "Xiao Xin sd hitting bricks", "matcher skill gw fighting", "Garfield cat ui Superman", "Xiao Xin yi hitting bricks", "mean yi I 2 ", "Current yt wire", "Apsara mobile ytui stroller", "secret character of Shen Dyu", "Fire yui Chai Ren S technology yui clever combat", "supervoice SDyu speed Battlefield ", "Little SD Xin brick uyi block", "Match yui human SD skills combat", "add yui Fei S cat Superman", "Little DF Xin brick ui block ", "Mean uyi FS I 2", "e-D streaming yui line", "Apsara SD manual uyi car", "God I secret SD character ", "matchwood human skill Dhk clever combat", "super sound ASD fast combat hk", "Xiao Xin beat SAhkD bricks", "matchwood human skill SD clever ghk combat ", "Garfield FDS Cat k Superman", "Little Xin beat SDF brick ytui block", "mean SDF yui I 2", "Current SDyuF wire", "Apsara hand yuiDF stroller ", "Shen iy mi SD person hk things", "Match uyi person skill AS skill hk fighting", "uyin hg fast war FS Field", "Xiao Xin SDF bricks hjk block ", "fireworks SDF skill hj skillful combat", "Garfield SDhk cat Superman"];

 

/* 100,000 random data records */

Var arr = [], num;

For (var I = 0; I <100000; I ++ ){

Num = Math. floor (Math. random () * 50 );

Arr. push (arr0 [num]);

}

 

 

Var t1 = new Date (). getTime (); console. log (t1); // start time

 

Arr. unique (); // deduplication

 

Var t2 = new Date (). getTime (); console. log (t2); // End Time

 

Console. log (t2-t1 );

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.