have been preparing for the interview for some time, so the blog is too long not updated, now the basic knowledge points are reviewed, and then share the interview with some common questions:
Go to the formal Internet company written test, interview there is a great probability of using JavaScript to implement the array to re-weight encoding problem:
such as: Meizu pen test;
This blog post on JS How to implement the array to re-organize 5 methods, and attach demo demo and source code.
1. Traversal Array method
The simplest way to do this is to create a new array, iterate through the incoming array, and the value is not placed in the new array; Note : Determine if the value is in the array's method "IndexOf" Is the ECMAScript5 method, IE8 the following does not support, need to write some compatible low-version browser code, the source is as follows:
// simplest array de-weight Unique1 (array) { var n = [ ]; // a new temporary array // traverse the current array for (var i = 0; i < array.length; I++) { // // if (n.indexof (Array[i]) = = -1 return n;}
//determine if the browser supports INDEXOF, IndexOf is the EcmaScript5 new method IE8 the following (including IE8, IE8 only support partial ECMA5) does not supportif(!Array.prototype.indexOf) { //New IndexOf MethodArray.prototype.indexOf =function(item) {varresult =-1, A_item =NULL; if( This. length = = 0){ returnresult; } for(vari = 0, Len = This. length; i < Len; i++) {A_item= This[i]; if(A_item = = =Item) {Result=i; Break; } } returnresult; }}
2. Object Key-value pair method
The method executes faster than any other method, which is to occupy a larger amount of memory; realize the idea: create a new JS object and an array, iterate over the incoming array, and determine if the value is the key of the JS object. If not, add the key to the object and put it in the new array. Note: When judging whether the JS object key, the incoming key will be automatically executed "toString ()", different keys may be mistaken, for example: a[1], a["1"]. To solve the above problem, you have to call "IndexOf".
//Fastest , occupying the most space (space change time)functionunique2 (array) {varn = {}, R = [], Len =Array.Length, Val, type; for(vari = 0; i < Array.Length; i++) {Val=Array[i]; Type=typeofVal; if(!N[val]) {N[val]=[Type]; R.push (Val); } Else if(N[val].indexof (type) < 0) {N[val].push (type); R.push (Val); } } returnR;}
3. array subscript judgment method
still have to call "IndexOf" performance is similar to Method 1, implementation ideas : If the current array of item I first appears in the current array where it is not I, That means that item I is repeated and ignored. Otherwise, the result array is deposited.
function Unique3 (array) { var// result array // start traversing from the second item for (var i = 1; i < array.length; i++) { // If the item I of the current array first appears in the current array is not I, // Then the term I is duplicated and ignored. Otherwise deposit the result array if (Array.indexof (array[i]) = = i) N.push (Array[i]); } return N;}
4. post-sorting neighbor removal &NBSP;
Although the "sort" method of the native array is not very reliable, it has no effect on the shortcomings of the sequential de-emphasis. implementation idea: sorts the incoming array, sorts the same values next to each other, and then iterates through the array with only the values that do not duplicate the previous value.
// next to the same value, and then traverse to remove duplicate values function unique4 (array) { array.sort (); var re=[array[0]]; for (var i = 1; i < array.length; i++) { if(Array[i]!== re[re.length-1]) { re.push (array[i]); } } return re;}
5. Optimized traversal Array Method
foreign blog implementation Ideas : Gets the right-most value that is not duplicated into the new array. (The next round of judgment that terminates the current loop and enters the top loop when a duplicate value is detected)
// idea: Get the right-most value without duplicates into a new array function Unique5 (array) { var r = []; for (var i = 0, L = array.length; i < L; i++) { for (var j = i + 1; j < L; J + +) if (array[i] = = = Array[j]) j = + +i; R.push (Array[i]); } return R;}
Implement Demo:
Http://wteamxq.com/testDemo/array.html
Demo Source:
Https://github.com/wteam-xq/testDemo/blob/master/array.html
Resources:
http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/
Front-end Interview Prerequisites: JS array de-weight algorithm implementation