First, interview 80% to ask the array to go heavy
There are many ways to get an array of weight, in fact, the interview is mainly to rely on the understanding of the object. Remember the first time I went to an interview, I used 2 for loops when I went to the heavy.
1 Cycles Once
varARR1 = [1,2,3,44,44,11,2,3,333,3,3]; functionRemoverepeat (arr) {varRemovearr = [],obj = {}; for(vari = 0, L = arr.length; I < L; i++){ if(!Obj[arr[i]]) {Removearr.push (arr[i]); Obj[arr[i]]= 1; } }; returnRemovearr; } varSinglearr =removerepeat (ARR1); Console.log (Singlearr); //[1, 2, 3, A, one, 333]
function Remove2 (arr) { return array.from (new Set (arr)); } var single2 = remove2 (arr1); Console.log (single2) //[1, 2, 3,, one, 333] es6 syntax actually ask the array of the de-weight is mainly admitted to the face of a way of thinking
The character that returns the most occurrences of a character in a string
varstr = "ABBCCCDDDDFFFFFGGGGGG"; functionMaxnum (str) {if(Str.length <= 1){ returnstr; } varA1 = Str.split ("), obj = {},result = []; //using the above an array to weigh the thought for(vark = 0, L = a1.length; K < L; k++){ if(!Obj[a1[k]]) {Obj[a1[k]]= 1; }Else{Obj[a1[k]]+ = 1; }; } varnum = 0, Word = ""; for(varIteminchobj) { if(Obj[item] >num) {num=Obj[item]; Word=item; } } returnWord; } varW =maxnum (str); Console.log (w); //g
Third, sorting algorithm (too many kinds of sorting algorithm)
1. Bubble sort
var a3 = [1,4,2,90,3,4]; for (var i = 0, L = a3.length; i < l-1; i++) { for (var j = i + 1; j < L; J++
>) {
if(A3[i] >
a3[j]) {
var temp =
a3[i]; =
A3[j]; =
temp;}} } Console.log (A3);
//
[1, 2, 3, 4, 4, all]
2. Select sort
var a3 = [1,4,2,90,3,4 for (var j = 0, L = a3.length; J < L; j ++ for (k = l; k > J; k--) { if (A3[j] > A3[k]) { var temp = A3[j]; A3[J] = a3[k]; A3[k] = temp; }}} console.log (A3); // //[1, 2, 3, 4, 4, all " // with bubble sort thinking a bit like, just keep up with the last one, such a round down, the order is complete.
Third, the value of 2 variables is not exchanged with intermediate variables
var a = 4,b = 5; function Change (b) { = B- A; = a + b; = A- b; return [A, b]; } Console.log (A, B) //The way of thinking is good. Anyway the first time, did not think [5,4]
Four, not to be continued.
Common algorithms in JS