# coding question

**Merge two arrays of objects**



```javascript
let first=[
            {uuid: 2, name: 'Darling'},
            {uuid: 3, name: 'Cry-Baby'},
            {uuid: 4, name: 'Snitch'},
            {uuid: 5, name: 'Pawn'}
        ];
 let second= [
            {uuid: 1, role: 'admin'},
            {uuid: 2, role: 'contributor'},
            {uuid: 3, role: 'owner'},
            {uuid: 4, role: 'contributor'}
            ];
//[
// {uuid:1, name: null, role: 'admin'},
// {uuid:2, name: 'Darling', role: 'contributor'},
// {uuid:3, name: 'Cry-Baby', role: 'owner'},
// {uuid:4, name: 'Snitch', role: 'contributor'},
// {uuid:5, name: 'Pawn', role: null}
//]
```

```
const mergeArrayObjects = (arr1, arr2) => {
  const res= [];
// start
  for (const obj of arr1) {
    const dup = arr2.find(
      a => a['uuid'] === obj['uuid'],
    );
    if (dup) {
      res.push(Object.assign(obj, dup));
    } else {
      res.push(obj);
    }
  }
  for (const obj of arr2) {
    const dup = res.find(
      a => a['uuid'] === obj['uuid'],
    );
    if (!dup) {
      res.push(obj);
    }
  }
  ///fill

  res.forEach(element => {
    if(!element.hasOwnProperty('name')) {
        element.name=null;
    }
    if(!element.hasOwnProperty('role')) {
        element.role =null;
    }
  });
  res.sort(function(a, b) { 
    return a.uuid - b.uuid;
  });
	console.log(res);
	
  return res;
}

```



Count array occurs highest

```
const numbers = [1,3,4,6,9,8,5,4,3,2,1,4,2,1,1,1];
function ca(num) {
	let count =  {};
  num.map(element => {
    //if(count.hasOwnProperty(element)) {
    if (element in count) {
    	count[element]++;
    } else {
    	count[element] = 1;
    }
  })
  let max = 0;
  let cur = 0;
  for(const key in count) {
  	if(count[key] > max) {
    	max = count[key];
      cur = key;
    }
  }
  return cur;
}

ca(numbers);
```



delete node in inkedlist

top two frequents in array

flattern object/array

####

```
function flatten(arr) {
  return arr.reduce((a, b) => {
    // return Array.isArray(b) ? a.concat(flatten(b)) : a.concat(b);
    return a.concat(Array.isArray(b) ? flatten(b) : b);
  }, []);
};

// es6
const flatten = arr =>
  arr.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
  
```



```
var foo = 1;
function bar() {
	foo = 10;
	return;
	function foo() {}
}
bar();
alert(foo);
```

```
function bar() {
    return foo;
    foo = 10;
    function foo() {}
    var foo = 11;
}
alert(typeof bar());
```

```
function foo(){}
delete foo.length;
alert(typeof foo.length);
```

closure:

&#x20; Fibonacci closure:

&#x20; use closure to change loop / use a closure to create a private counter?

&#x20;closure in callback



Leetcode

**1  2sum**

**3 string/array  use map**[  **Longest Substring Without Repeating Characters**](https://leetcode.com/problems/longest-substring-without-repeating-characters)&#x20;

&#x20;  **use a duplicateIndex = \[];**&#x20;

**15 3sum** [**3Sum Closes**](https://leetcode.com/problems/3sum-closest)**t**  [**3Sum With Multiplicity**](https://leetcode.com/problems/3sum-with-multiplicity)   &#x20;

&#x20;   **for loop,  left = i+ 1; right = length -1; while(left < right)....if else**

**20**  [**Valid Parentheses**](https://leetcode.com/problems/valid-parentheses)   &#x20;

&#x20;     **push ), ], } into array, use pop();**

**26**  Remove Duplicates from Sorted Array

&#x20;   nums.splice(i, 1); i--;

**27** [**Remove Element**](https://leetcode.com/problems/remove-element)   &#x20;

&#x20;   **nums.splice(i, 1), i--;**

**33**[  **Search in Rotated Sorted Array**](https://leetcode.com/problems/search-in-rotated-sorted-array)  &#x20;

&#x20;   **toxic**&#x20;

**36  Valid Sudoku**

**39**  [**Combination Sum**](https://leetcode.com/problems/combination-sum)&#x20;

&#x20;      **iterative, help(index + 1, array, target - candidates\[index]);**

**46** [**Permutations**](https://leetcode.com/problems/permutations)   &#x20;

**48** [**Rotate Image**](https://leetcode.com/problems/rotate-image)   &#x20;

**49**

**73** [**Set Matrix Zeroes**](https://leetcode.com/problems/set-matrix-zeroes)&#x20;

**80** [**Remove Duplicates from Sorted Array II**](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii)  &#x20;

&#x20;  **splice(), O(n)**

**88** [**Merge Sorted Array**](https://leetcode.com/problems/merge-sorted-array)   &#x20;

**94** [**Binary Tree Inorder Traversal**](https://leetcode.com/problems/binary-tree-inorder-traversal)  &#x20;

**102** [**Binary Tree Level Order Traversal**](https://leetcode.com/problems/binary-tree-level-order-traversal)   &#x20;

**121** array / dp  [**Best Time to Buy and Sell Stock**](https://leetcode.com/problems/best-time-to-buy-and-sell-stock)  &#x20;

&#x20;       **tempmin, tempmax, res. for loop ON/// try to use reduce**

```
    let minPrice = +Infinity;
```

**\*\*\*\*\* 125 Valid Palindrom**&#x20;

let str = s.replace(/\W/g, '').toLowerCase();

**144** [**Binary Tree Preorder Traversal**](https://leetcode.com/problems/binary-tree-preorder-traversal)   &#x20;

**151** [**Reverse Words in a String**](https://leetcode.com/problems/reverse-words-in-a-string)  &#x20;

**155** [**Min Stack**](https://leetcode.com/problems/min-stack)  &#x20;

**192** [**Word Frequency**](https://leetcode.com/problems/word-frequency)   &#x20;

**203** [**Remove Linked List Elements**](https://leetcode.com/problems/remove-linked-list-elements)&#x20;

**204**[ **Count Primes**](https://leetcode.com/problems/count-primes)

**206** [**Reverse Linked List**](https://leetcode.com/problems/reverse-linked-list) &#x20;

**238** [**Product of Array Except Self**](https://leetcode.com/problems/product-of-array-except-self)   &#x20;

**268** [**Missing Number**](https://leetcode.com/problems/missing-number) &#x20;

&#x20;      **use sum, reduce get array sum, diff.**

**283** [**Move Zeroes**](https://leetcode.com/problems/move-zeroes)   &#x20;

**from back to front.**

```
var moveZeroes = function(nums) {       
    for(var i = nums.length;i--;){
        if(nums[i]===0){
            nums.splice(i,1)
            nums.push(0);
        }
    }
};
```

**300**  [**Longest Increasing Subsequence**](https://leetcode.com/problems/longest-increasing-subsequence)

**316**  [**Remove Duplicate Letters**](https://leetcode.com/problems/remove-duplicate-letters)&#x20;

**349** [ **Intersection of Two Arrays**](https://leetcode.com/problems/intersection-of-two-arrays)   &#x20;

**387 String  First Unique Character in a String**

&#x20;     **2 pass;**

**394** [ **Decode String**](https://leetcode.com/problems/decode-string)  &#x20;

**509 Fibonacci closure**

**819** [Most Common Word](https://leetcode.com/problems/most-common-word)   &#x20;







## Use array/Object for any question.

string =  array.join("");

no need queue, stack, map
