Interview question of Bank of Communications Pacific Credit Card Center: How do arrays go heavy?

Source: Internet
Author: User
Tags array length

7 ways to implement an array to re-preface

Go to the weight is often encountered in the development of a hot issue, but the current project encountered in the situation is the background interface using SQL to heavy, simple and efficient, the basic will not let the front-end processing to heavy.

So what happens to the front-end processing? If each page shows 10 different data, if the data duplication is serious, then to display 10 data, it may be necessary to send multiple HTTP requests to filter out 10 different data, and if the background is heavy, only one HTTP request can get to 10 different data.

Of course, this is not to say that the front-end weight is not necessary, still need to be skilled use. This paper mainly introduces several common methods of array de-weight.

method to realize double cyclic de-weight

A double for (or while) loop is a clumsy way to implement a simple principle: first define an array containing the first element of the original array, then iterate through the original array, match each element in the original array to each element in the new array, add to the new array if it is not repeated, and return the new array at the end Because its time complexity is O (n^2), if the array length is large, then it will be very memory-intensive.

function Unique (arr) {if(!Array.isarray (arr)) {Console.log (' Type error! ')        return} let Res= [Arr[0]]     for(Let i = 1; i < arr.length; i++) {Let flag=true         for(Let j = 0; J < Res.length; J + +) {            if(Arr[i] = = =Res[j]) {Flag=false;  Break            }        }        if(flag) {Res.push (Arr[i])}}returnRes}
IndexOf method to weigh 1

The IndexOf () method of an array returns the position of the first occurrence of a specified element in the array. The method first defines an empty array, res, and then calls the IndexOf method to traverse the original array, and if the element is not in res, push it into the RES and finally return the res to get the array of the de-weight.

function Unique (arr) {    if (!  Array.isarray (arr)) {        Console.log (' type error! ') )        return    }    = []    for (Let i = 0; i < arr.length; I + +)        {if (Res.indexof (arr[i]) = =-1) {            res.push (Arr[i])        }    }     return  res}

IndexOf method to weigh 2 (This method does not understand??? )

Use IndexOf to detect whether the first occurrence of an element in an array is equal to the position of the element now, and if not equal, the element is a repeating element.

function Unique (arr) {    if (!  Array.isarray (arr)) {        Console.log (' type error! ') )        return    }    return  Array.prototype.filter.call (arr, function (item, index) {        return arr.indexof (item) = = = index;    });
Adjacent element de-weight

This method first calls the array sorting method sort (), and then iterates through the sorted results and the neighboring elements, and if equal skips the elements until the end of the traversal.

function Unique (arr) {    if (!  Array.isarray (arr)) {        Console.log (' type error! ') )        return    }    = arr.sort ()    = [arr[0]]      for (Let i = 1; i < arr.length; i++) {        if (Arr[i]!== arr[i-1]) {            Res.push (a Rr[i])        }    }    return  res}

Use object properties to remove weight (understand the 80%.)

Create an empty object, iterate over the array, set the value in the array to the object's property, and assign the property an initial value of 1, each time the corresponding property value is incremented by 1, so that the attribute value corresponds to the number of occurrences of the element.

 function unique (arr) { if  (!array.isarray (arr) {Console.log ( ' type error! ')    )  return  } Let res  = [], obj  = {}  for  (Let i = 0; i < arr.length; I++ if  (! Obj[arr[i]] {res.push (arr[i]) obj[arr[i]]  = 1}  else   ++}}  return   res}  
Set and deconstruction assignment de-weight

One of the biggest features of the new data type Set,set in ES6 is that data is not duplicated. The SET function can accept an array (or class array object) as a parameter to initialize, and this feature can also be used to do to the array to weight.

function Unique (arr) {    if (!  Array.isarray (arr)) {        Console.log (' type error! ') return    }    return [... ]         New Set (arr)]}

Array.from and set de-weight

The Array.from method transforms the set structure into an array result, and we know that the set result is a data set that is not duplicated, and therefore can achieve the purpose of deduplication.

function Unique (arr) {    if (!  Array.isarray (arr)) {        Console.log (' type error! ') )        return    }    return array.from (new  Set (arr))} 

Summarize

The problem of array de-weight is a hot issue that is often encountered in development. We can choose different implementations according to different application scenarios.

Interview question of Bank of Communications Pacific Credit Card Center: How do arrays go heavy?

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.