coding question
Merge two arrays of objects
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:
Fibonacci closure:
use closure to change loop / use a closure to create a private counter?
closure in callback
Leetcode
1 2sum
3 string/array use map Longest Substring Without Repeating Characters
use a duplicateIndex = [];
15 3sum 3Sum Closest 3Sum With Multiplicity
for loop, left = i+ 1; right = length -1; while(left < right)....if else
push ), ], } into array, use pop();
26 Remove Duplicates from Sorted Array
nums.splice(i, 1); i--;
nums.splice(i, 1), i--;
33 Search in Rotated Sorted Array
toxic
36 Valid Sudoku
iterative, help(index + 1, array, target - candidates[index]);
46 Permutations
48 Rotate Image
49
80 Remove Duplicates from Sorted Array II
splice(), O(n)
94 Binary Tree Inorder Traversal
102 Binary Tree Level Order Traversal
121 array / dp Best Time to Buy and Sell Stock
tempmin, tempmax, res. for loop ON/// try to use reduce
let minPrice = +Infinity;
***** 125 Valid Palindrom
let str = s.replace(/\W/g, '').toLowerCase();
144 Binary Tree Preorder Traversal
155 Min Stack
192 Word Frequency
203 Remove Linked List Elements
204 Count Primes
238 Product of Array Except Self
268 Missing Number
use sum, reduce get array sum, diff.
283 Move Zeroes
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
349 Intersection of Two Arrays
387 String First Unique Character in a String
2 pass;
394 Decode String
509 Fibonacci closure
819 Most Common Word
Use array/Object for any question.
string = array.join("");
no need queue, stack, map
Last updated
Was this helpful?