Javascript array deduplication _ javascript tips

Source: Internet
Author: User
Tags javascript array
This article summarizes four methods of array deduplication in javascript, which are loop matching deduplication, JSON deduplication object deduplication dictionary deduplication, and queue recursive deduplication, the INDEXOF deduplication method is very detailed and comprehensive. If you need it, you can refer to it. Preface

Recently, in order to change jobs, I prepared for an interview and began to review JavaScript-related knowledge. Yesterday afternoon I thought of array deduplication-related methods. I simply compiled several JavaScript algorithm articles for future use, in this series of articles, there may be no number of articles, no time, no time to write, no correctness, and no efficiency. I just want to talk about my understanding. If there is any mistake, please make an axe.

About deduplication

Array deduplication is a common algorithm inspection point. Unique and non-unique methods are used to remove duplicates. Simply put, you can pick out the unique one or delete the unique one. All of the following algorithms are named by myself. Ignore them.

Loop matching deduplication

As the name suggests, each element in the array is compared with the array storing elements. When an element that is not repeated is encountered, it is placed into the new array until the loop ends, because the matching also has a loop, it can also be called double loop matching deduplication, which is also the simplest method that everyone can think.

Implementation Code:

Var arr = [,]; var result = []; // match function isMatch (array, n) {for (var I = 0; I

Note: There is a bug in the above method. When there is a string in the form of numbers and numbers, there is no distinction between a number and a number string. Because the matching function isMatch () uses a pair of equal "=" and does not validate the element type, we should actually use equal "= ".
The modified code is as follows:

Var arr = [, 3, '1', 7]; var result = []; // matches function isMatch (array, n) {for (var I = 0; I

Algorithm advantages and disadvantages:

Advantages:

Simple implementation and intuitive thinking

Disadvantages:

Low Efficiency

JSON deduplication/object deduplication/dictionary deduplication

JSON deduplication is simply to use the uniqueness of the Object key to convert the array elements to JSON or the Object key value. The JSON value stores the index of the array, and then performs a for in loop on the JSON object to store it in the new array.

Array, JSON, and {} are all objects, so this algorithm can be implemented using any one.

Implementation Code:

Array method:

Var arr = [, 3, '1', 7]; function unqiue (array) {var cache = []; var result = []; // convert the array element into the object's key for (var I = 0; I

JSON mode:

Var arr = [, 3, '1', 7]; function unqiue (array) {var cache ={}; var result = []; // convert the array element into the object's key for (var I = 0; I

Object method:

Var arr = [, 3, '1', 7]; function unqiue (array) {var cache = new Object (); var result = []; // convert the array element into the object's key for (var I = 0; I

Algorithm advantages and disadvantages:

Advantages:

Simple

High Efficiency

Disadvantages:

1. Changed the type of array elements ()
2. There is a bug (it is nothing more than a string of numbers and numbers)

Recursive deduplication of queues

Last night, I thought for a long time that I would like to use the queue method to sort the arrays into ascending or descending queues so that the same elements are in a region and then match them forward from the end of the team, if the match succeeds, the team end is deleted and the previous element matches the previous one. After the matching is complete, the remaining elements are the deduplicated queues.

Var arr = [6, 4, 6, 9, '6', 13, 56, 9, '11', 1, 8, '7', 17, 5, 45, 3, 7]; function unqiue (array) {// sort the array to form the queue array. sort (function (m, n) {return m-n ;}); // after sorting, the team end is compared forward. If the same, delete the team end, function loop (Index) {if (Index> = 1) {if (array [Index] = array [Index-1]) {arr. splice (Index, 1);} loop (Index-1);} loop (array. length-1); return array;} console. log (unqiue (arr ));

Algorithm advantages and disadvantages:

Advantages:

High Efficiency

Disadvantages:

Inefficient

INDEXOF deduplication Method

Determine whether the browser supports indexOf. If indexOf is ecmaScript5, the new method IE8 or lower (IE8 and IE8 only support part of ecma5) is not supported.

If (! Array. prototype. indexOf) {// added the indexOf method Array. prototype. indexOf = function (item) {var result =-1, a_item = null; if (this. length = 0) {return result;} for (var I = 0, len = this. length; I <len; I ++) {a_item = this [I]; if (a_item = item) {result = I; break ;}} return result ;}}
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.