two sum javascript
var twoSum = function(nums, target) {
//hash table
var hash = {};
for(let i=0; i<=nums.length; i++){
//current number
var currentNumber = nums[i];
//difference in the target and current number
var requiredNumber = target - currentNumber;
// find the difference number from hashTable
const index2 = hash[requiredNumber];
// if number found, return index
// it will return undefined if not found
if(index2 != undefined) {
return [index2, i]
} else {
// if not number found, we add the number into the hashTable
hash[currentNumber] = i;
}
}
};function sumArrays(...arrays) {
const n = arrays.reduce((max, xs) => Math.max(max, xs.length), 0);
const result = Array.from({ length: n });
return result.map((_, i) => arrays.map(xs => xs[i] || 0).reduce((sum, x) => sum + x, 0));
}
console.log(...sumArrays([0, 1, 2], [1, 2, 3, 4], [1, 2])); // 2 5 5 4
Also in JavaScript:
- convert shp to geojson python
- node js try catch
- js string slicing
- update url parameters and create history entry
- list all node processes
- cache buster in angular application
- Embed Example Discord.js
- check if modal hide jquery
- jquery number format comma
- how to do basic authentication with fetch api
- jquery table each rows with class
- not operator js
- escaped json to json javascript
- javascript array flat
- preload images javascript
- open google map with latitude and longitude javascript
- get an html img tag from a string
- javascript object entries
- javascript diffence between a++ and ++a
- textinput onpress react native
- redirect through javascript
- compare two arrays
- javascript dynamic arrays
- this.setstate is not a function in react native