50-Channel JS ability Evaluation classic problems and solutions

Source: Internet
Author: User
Tags shallow copy

Recently in the study of "data structure and algorithm JavaScript description" This book, the characteristics of JavaScript and data structure have a further understanding and experience.

Learning, but also carried out the corresponding exercises, the problem is not very difficult, but the knowledge of the consolidation is very helpful, so here and everyone do a share.

Note: The title is mainly derived from the cattle network , which is a good site for the code of learning and self-ability of the test is very helpful, interested friends can go to see.

Most of these problem solving is based on their own ideas, and almost all adopt a variety of methods, some methods refer to the site of other friends of the code, if

There is a better solution is also welcome to put forward, I will make a detailed supplement. If there are obvious mistakes, please spray them lightly.

The first is the part of the array, the operation of the array in JavaScript is described in more detail in the previous blog "javascipt operation Array " , if the following

The solution of the problem in the face of a friend who has doubts can go and see.

Talk is cheap,show me the code!

"Array"

One, move the divisor group all the values in arr are equal to the item element. Do not directly modify the array arr, and the result returns a new array.

Method One  uses the Filter method function remove (arr, item) {return Arr.filter (function (x) {return (X!=item);})} Method Two  creates a new array that will delete the specified element after the remaining elements are stored in this array and return function remove (Arr,item) {var newArr = [];//var index = 0;for (var i in arr) {if (arr[ I] = Item) {Newarr.push (arr[i]);//newarr[index++] = Arr[i];}} return NEWARR;} Method three   shallow copy the original array and use the splice () method on the new array to remove the specified element function remove (arr, item) {var NEWARR = arr;for (var i = 0; i < newarr.length ; i + = 1) {if (newarr[i] = = = Item) {Newarr.splice (I, 1);}} return NEWARR;}

Second, move the divisor group all the values in arr are equal to the item, operate directly on the given ARR array

function Removewithoutcopy (Arr,item) {for (Var i=0;i<arr.length;i++) {if (Arr[i]===item) {arr.splice (i,1); i--;// Because each time an element is deleted, the array length is reduced by one, so I need to subtract one}}return arr;}

Third, add the element item at the end of the array arr. Do not directly modify the array arr, the result returns a new array

Method one concat () The method does not alter the existing array, but simply returns a copy of the concatenated array. function Append (arr,item) {return Arr.concat (item);} Method two slice (0) copies the array function append (arr, item) {    var newArr = arr.slice (0);//copy array    Newarr.push (item);//Add Element    return A;} Method three adds an element to the array function append (Arr,item) {var newArr = new Array (); for (Var i=0;i<arr.length;i++) {Newarr[i] = arr[i];} Newarr[newarr.length] = Item;return newArr;}

Iv. Delete the last element of the array arr. Do not directly modify the array arr, the result returns a new array

/method One directly assigns the 1 to length-1 element of the original array to the new array and returns the function truncate (arr) {var newArr = [];for (var i=0;i<arr.length-1;i++) {Newarr[i ] = Arr[i];} return NEWARR;} Method two slice (start,end)  returns the selected element function truncate (arr) {return Arr.slice (0,arr.length-1) from the array;} Method three push () function truncate (arr) {var newArr = [];for (var i in arr) {if (i! = arr.length-1) {Newarr.push (arr[i]);}} return NEWARR;}

V. Add an element to the beginning of the array arr item. Do not directly modify the array arr, the result returns a new array

Method one creates a new array and assigns the first element a value of Itemfunction prepend (arr, item) {var NEWARR = [];newarr[0] = Item;for (var i=0;i<arr.length;i++) {n EWARR[I+1] = Arr[i];} return NEWARR;} Method Two resembles method one, but connects directly with Concat NEWARR and Arrfunction prepend (arr,item) {var newArr = [];newarr[0] = Item;//newarr.push (item); Newarr.concat (arr); return NEWARR;} Method three Unshift () method function Preaend (arr,item) {var newArr = arr.slice (0); return newarr.unshift (item);}

Vi. Delete the first element of an array of arr. Do not directly modify the array arr, the result returns a new array

Method One: Directly assigns the element 1~ (length-1) of the original array to the new array and returns the function Delhead (arr) {var newArr = [];for (var i=0;i<arr.length-1;i++) { Newarr[i] = arr[i+1];} return NEWARR;} Method two shift () method function Delhead (arr) {var newArr = arr.slice (0); Newarr.shift (); return NEWARR;} Method three  directly intercepts the second to last element of Arr to the new array function Delhead (arr) {var newArr = Arr.slice (1); return NEWARR;}

Merge arrays arr1 and Arrays arr2. Do not directly modify the array arr, the result returns a new array

Method One: Use the concat () method function Concat (ARR1,ARR2) {var arr = arr1.concat (ARR2); return arr;} Method Two: Insert all elements in ARR2 into arr1 function concat (arr1,arr2) {var newArr = new Array (); for (Var j=0;j<arr1.length;j++) { NEWARR[J] = arr1[j];} for (Var i=0;i<arr2.length;i++) {Arr[j+i] = arr2.[ I];} return arr;} Method Three: Add the contents of arr1 and arr2 to the new array respectively function concat (ARR1,ARR2) {var newArr = [];for (var i in arr1) {Newarr.push (arr1[i]);} FMR (Var j in arr2) {Newarr.push (arr2[j])}return NEWARR;}

Add an element to the index of ARR at array item. Do not directly modify the array arr, the result returns a new array

Method One: Use the splice (Index,n,item) method function Insert (arr, item, index) {var newArr = arr.slice (0); Newarr.splice (Index,0,item ); return NEWARR;} Method Two: Split the original array at index and add item assignment to the new Array function insert (Arr,item,index) {var newArr = new Array (); NEWARR = Arr.slice (0,index). Concat (item). Concat (Arr.slice (index,arr.length));//NewArr = Arr.slice (0,index). push (item). CONCAT (Arr.slice (Index , arr.length)); return NEWARR;} Method Three: Similar to Method two, the new array is assigned multiple times function Insert (Arr,item,index) {var newArr = [];for (var i=0;i<index;i++) {Newarr[i] = arr[i];} Newarr[index] = Item;for (var j=index;j<arr.length;j++) {newarr[j+1] = Arr[j];}}

Ix. statistics array The number of occurrences of an element in arr that is equal to item

Method One: The elements in the array are judged individually, if the value equals item, then the Count plus 1function count (arr,item) {var num = 0;for (var i=0;i<arr.length;i++) {if (arr[i]== =item) {num++;}} return num;} Method Two: Similar to method one, use the ForEach () function count (arr,item) {var num = 0;arr.foreach (function (elem) {if (Elem===item) {num++;}}) return num;} Method Three: Use the Splice () method to determine the element one by one (slightly cumbersome) function count (arr,item) {var num = 0;while (arr.indexof (item)!==-1) {   // That is, find the element with the value equal to item Arr.splice (Arr.indexof (item), 1);//Find the element to delete, continue to judge the remaining elements num++;}}

X. Find the repeating elements in the array arr

Method One: If the index of the first and last occurrence of an element is compared, if not equal is repeated, if the new array is not stored this element is stored function duplicates (arr) {var result = [];    Arr.foreach (function (elem) {       if (Arr.indexof (elem)!=arr.lastindexof (elem) && result.indexof (elem) = =-1) {           Result.push (elem);       }    });    return result;} Method Two: First determine whether the value of the array is equal to the index of X is unique, if unique if the new array does not appear in the new Array function duplicates (arr) {var newArr = [];for (Var i=0;i<arr.length;i + +) {if (Arr.indexof (Arr[i])!==i) {//The element in the array whose value is a value is not unique if (Newarr.indexof (Arr[i]) ===-1) {//Jovin array does not store this element Newarr.push (Arr[i] );}}} return NEWARR;}

For each element in an array of arr, ask for two times. Do not directly modify the array arr, the result returns a new array

Method One: The elements in the array are squared function square (arr) {var newArr = arr.slice (0); for (Var i=0;i<newarr.length;i++) {Newarr[i] = Newarr[i] * newarr[i];} return NEWARR;} Method Two: Similar to method one, the code is more concise function square (arr) {var newArr = arr.slice (0); for (var i in NEWARR) {newarr[i]*=newarr[i];} return NEWARR;}

12. In the array arr, find the position where the element with the value equal to item appears

Method One: Find the element that is equal to the item value and return Indexfunction findalloccurrences (arr, target) {var index = [];for (var i in arr) {if (arr[i]=== Target) {Index.push (i);}} return index;} Method Two: Meet indexof (target)! =-1 element Output indexfunction findallocurrences (arr, target) {var index = [];var i = Arr.indexof (Targte);//i is the first element to satisfy the required position while ( I!==-1) {Index.push (i); i = Arr.indexof (target,i+1);} return index;}

50-Channel JS ability Evaluation classic problems and solutions

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.