shuffle array without consecetive item - Javascript - javascript

I currently acheive shuffle one of array without duplicated with previous value of next value.
However, I don't think my method is good. I think there are better way.
Firstly, I will describe what the array looks like.
it's an array of object with property {answer,url,category} and there are duplicated item in the array. think as [A,A,B,B,C,C] , A,B,C as object here.
As EzioMerce pointed out, in this array, object will always has equal amount of numbers. such if there are 3 A, will definitely have 3 B and C. It will not have array as such [A,A,A,B]
I need the array to shuffle until there is no consecutive object next to each other such as [A,B,C,B,A,C]
Here is my solution (which I have tested 40 times without consecutive object):
getShuffleWords(array: Card[]) {
//First shuffle..
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor( Math.random() * (i - 1));
if (j > 0) {
//make sure there are no simliar value around position i and j
if (array[i]?.answer !== array[j - 1]?.answer
&& array[i]?.answer !== array[j + 1]?.answer
&& array[i]?.answer !== array[j - 1]?.answer
&& array[i]?.answer !== array[j + 1]?.answer) {
[array[i], array[j]] = [array[j], array[i]];
}
} else {
if (array[i]?.answer !== array[j + 1]?.answer) {
[array[i], array[j]] = [array[j], array[i]];
}
}
}
const before = [...array];
console.log(before);
//Checking duplicate and swap with other value
for (let x = 0; x < array.length; x++){
if (array[x].answer == array[x + 1]?.answer) {
console.log(x,array[x])
for (let y = 0; y < array.length; y++){
//make sure there are no simliar value around position x and y
if (array[y]?.answer !== array[x]?.answer
&& array[y + 1]?.answer !== array[x].answer
&& array[y - 1]?.answer !== array[x].answer
&& array[y]?.answer !== array[x+1]?.answer
&& array[y]?.answer !== array[x-1]?.answer
) {
console.log(y, array[y]);
if (x < y) {
[array[x], array[y]] = [array[y], array[x]];
} else {
[array[y], array[x]] = [array[x], array[y]];
}
break;
}
}
}
}
//just another caculate if there is duplication left (for testing purpose)
let dup = 0;
for (let x = 0; x < array.length; x++){
if (array[x].answer == array[x + 1]?.answer) {
dup++;
}
}
console.log(array, dup);
return array;
}
However, in the //First shuffle.. there are always has some consecutive object exisited in the array. thats why I repeat another checking and replace the value.
How would I improve my method. and why my first shuffle does not work perfectly?

You can try this way. It also works if arr will be an array of objects
const arr = [1, 2, 3, 1, 2, 3, 1, 2, 3];
const shuffle = (arr) => {
const uniqueArr = [...(new Set(arr))];
const shuffledArr = [];
let countOfOneObj = 0;
for (const obj of arr) {
if (obj === arr[0]) ++countOfOneObj;
};
const getRandomObj = () => {
const i = Math.floor(Math.random() * uniqueArr.length);
return uniqueArr[i];
}
for (let i = 0; i < countOfOneObj; ++i) {
let usedObjs = []
for (let j = 0; j < uniqueArr.length; ++j) {
const obj = getRandomObj();
if (
usedObjs.includes(obj) ||
shuffledArr[shuffledArr.length - 1] === obj
) {
--j;
continue;
}
usedObjs.push(obj);
shuffledArr.push(obj);
}
usedObjs = [];
}
return shuffledArr;
}
console.log(shuffle(arr));

You could shuffle an array of indices of same grouped values until you got a result which has no same neighbour indices.
const
fy = array => { // fisher-yates, adapted from https://stackoverflow.com/a/59837259/1447675
let i = array.length;
while (--i) {
const j = Math.floor(Math.random() * (i + 1));
[array[j], array[i]] = [array[i], array[j]];
}
},
shuffle = array => {
do fy(array);
while (array.some((v, i, a) => v === a[i + 1]))
};
indices = [0, 0, 0, 1, 1, 1, 2, 2];
shuffle(indices);
console.log(...indices);

Related

How do I continue my while loop in relations to quicksort

This is my array
numArray: [4, 2, 6, 7, 5, 1, 8, 3]
Here I create my pivot element which I chose to be the middle one
quickSort(array = this.state.numArray) {
const pivot = array[Math.floor(array.length / 2)]; //pivot = 5
const leftArr = [];
const rightArr = [];
next up I created a function that filters bigger and smaller numbers than 5
in the left and right array(smaller numbers in left and bigger in right)No Problem so far..
const Partition = () => {
for (let i = 0; i < array.length; i++) {
if (array[i] === pivot) {
console.log(array[i]);
} else if (array[i] < pivot) {
leftArr.push(array[i]);
} else {
rightArr.push(array[i]);
}
}
}
My swap function that, which swaps elements in the array
const swap = (items, leftIndex, rightIndex) => {
let temp = items[leftIndex];
items[leftIndex] = items[rightIndex];
items[rightIndex] = temp;
}
Last but not least my sorting function. Problem here is it stops sorting way too early since
the condition of the while loop is already met. How can I structure it so it keeps going till it's sorted?
const sort = (items) => {
let i = 0;
let j = items.length - 1;
let newPivot = items[0];
let sortedArray = items;
while (i <= j) {
while (sortedArray[i] < newPivot) {
i++;
}
while (sortedArray[j] > newPivot) {
j--;
}
if (i <= j) {
swap(sortedArray, i, j);
i++;
j--;
}
console.log(i + " <= " + j);
}
return sortedArray;
}

Issue while making a copy of 2D Array

My target here is to find 'N' for a 2D Array.
'N' = sum of corner elements * sum of non corner elements.
For 'N' calculation I change String & Boolean elements to their ASCII, 1 or 0 respectively. But my original array gets altered in this process.
Can't understand why?
function findN(arr) {
var temp = [...arr]
// first we change all elements to numbers
for (let i = 0; i < temp.length; i++) {
for (let j = 0; j < temp.length; j++) {
if (typeof temp[i][j] == 'string') {
temp[i][j] = temp[i][j].charCodeAt()
} else if (temp[i][j] == true) {
temp[i][j] = 1
} else if (temp[i][j] == false) {
temp[i][j] = 0
}
}
}
// N calculation starts here
let r = temp.length // rows
let c = temp[0].length // columns
var corner_Sum =
temp[0][0] + temp[0][c - 1] + temp[r - 1][0] + temp[r - 1][c - 1]
var total_Sum = 0
for (let i = 0; i < temp.length; i++) {
for (let j = 0; j < temp.length; j++) {
total_Sum = total_Sum + arr[i][j]
}
}
var N = corner_Sum * (total_Sum - corner_Sum)
return N
}
findN() ends here. It should return 'N', without altering the original array. As all calculations were done on temp array. But that's not the case.
Your problem is because arr is an array of arrays; when you copy it using
temp = [...arr]
temp becomes an array of references to the same subarrays in arr. Thus when you change a value in temp it changes the corresponding value in arr. You can see this in a simple example:
let arr = [[1, 2], [3, 4]];
let temp = [...arr];
temp[0][1] = 6;
console.log(arr);
console.log(temp);
To work around this, use a deep copy such as those described here or here. For example, if arr is at most 2-dimensional, you can nest the spread operator:
let arr = [[1, 2], [3, 4]];
let temp = [...arr.map(a => [...a])];
temp[0][1] = 6;
console.log(arr);
console.log(temp);

JavaScript Quicksort recursive

I´m trying to implement a quicksort algorithm for an int array in JavaScript.
I´ve got a problem in my code. The first few ints get sorted well but at the end of the sortet array there is always one integer which is placed many times although its only one time in the array which should be sorted. Hopefully someone will find my fault.
Thanks.
function quicksort(array) {
var randomPlace = Math.round(Math.random() * array.length);
var pivotelement = array[randomPlace];
left = new Array;
right = new Array;
for (var i = 0; i < array.length; i++) {
if (array[i] < pivot) {
left[left.length] = array[i];
} else {
right[right.length] = array[i];
}
}
if ((left.length == 0 || left.length == 1) && (right.length == 0 || right.length == 1)) {
return left.concat(right);
} else if (left.length == 0 || left.length == 1) {
return (left.concat((quicksort(right))));
} else if (right.length == 0 || right.length == 1) {
return ((quicksort(left)).concat(right));
} else {
return (quicksort(left)).concat((quicksort(right)));
}
}
Beside some nameming confusion, like pivotelement vs pivot and Math.round vs Math.floor, you need to tackle the problem of for example [1, 1, 1] which is always returning left = [] and right = [1, 1, 1], that calls quicksort([1, 1, 1]) ad infinitum.
To overcome this problem, you need to check for empty left and with right every element, if it is equal to the random pivot. Then return right, without calling quicksort again.
function quicksort(array) {
var randomPlace = Math.floor(Math.random() * array.length),
pivot = array[randomPlace],
left = [],
right = [],
i;
for (i = 0; i < array.length; i++) {
(array[i] < pivot ? left : right).push(array[i]);
}
console.log(pivot, JSON.stringify(array), JSON.stringify(left), JSON.stringify(right));
// prevent looping forever
if (!left.length && right.every(function (v) { return v === pivot; })) {
return right;
}
if (left.length <= 1 && right.length <= 1) {
return left.concat(right);
}
if (left.length <= 1) {
return left.concat(quicksort(right));
}
if (right.length <= 1) {
return quicksort(left).concat(right);
}
return quicksort(left).concat(quicksort(right));
}
console.log(quicksort([2, 7, 4, 8, 3, 11, 49, 20, 10, 1, 1, 1]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Another solution would be to separate the array into three arrays, one for smaller values, one for equal values and one for greater values. Then sort only the smaller and greater arrays.
function quicksort(array) {
var randomPlace = Math.floor(Math.random() * array.length),
pivotValue = array[randomPlace],
left = [],
pivot = [],
right = [],
i;
for (i = 0; i < array.length; i++) {
if (array[i] === pivotValue) {
pivot.push(array[i]);
continue;
}
(array[i] < pivotValue ? left : right).push(array[i]);
}
console.log(pivotValue, JSON.stringify(array), JSON.stringify(left), JSON.stringify(pivot), JSON.stringify(right));
if (left.length <= 1 && right.length <= 1) {
return left.concat(pivot, right);
}
if (left.length <= 1) {
return left.concat(pivot, quicksort(right));
}
if (right.length <= 1) {
return quicksort(left).concat(pivot, right);
}
return quicksort(left).concat(pivot, quicksort(right));
}
console.log(quicksort([2, 7, 4, 8, 3, 11, 49, 20, 10, 1, 1, 1]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
heres a short version of quick sort written in JS exactly as it is seen in Intro To Algorithms, hope this helps!
var partition = function (arr, low, high) {
var x = arr[high]
var i = low - 1
for (var j = low; j <= high - 1; j++) {
if (arr[j] <= x) {
i++
var temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
var temp = arr[i + 1]
arr[i + 1] = arr[high]
arr[high] = temp
return i + 1
}
var quickSort = function (arr, low, high) {
if (low < high) {
index = partition(arr,low,high)
if (low < index-1) quickSort(arr,low,index-1)
if (index+1 < high) quickSort(arr,index+1,high)
}
}
var list2 = [1000,13,12,1001,82,1,2,4,3,0]
console.log(quickSort(list2,0,list2.length))
also on my GitHub
I think you are identifying the randomPlace wrongly. It returns undefined some times because you're using Math.round().
Try this instead:
var randomPlace = Math.floor(Math.random() * array.length);
Also, use the following code when initializing left, and right:
var left = new Array();
var right = new Array();
Also, you need to change the pivot in array[i] < pivot to pivotElement.
You can see my complete fiddle here

What is the best way to shift a multidimensional array in Javascript?

I'm trying to create a javascript function that shifts an array right x units any up y units. It must keep the array size the same, and it must call unloadChunk for elements that are getting shifted off the multidimensional array. Here is my current implementation:
function shift(x, y) {
if (x > 0) {
for (var i = 0; i < chunks.length; i++) {
for (var j = chunks[i].length - 1; j >= 0; j--) {
if(j + x > chunks[i].length - 1 && chunks[i][j]) {
unloadChunk(i, j);
}
if (j < x) {
chunks[i][j] = null;
}
else {
chunks[i][j] = chunks[i][j - x];
}
}
}
}
else if (x < 0) {
for (var i = 0; i < chunks.length; i++) {
for (var j = 0; j < chunks[i].length; j++) {
if(j + x < 0 && chunks[i][j]) {
unloadChunk(i, j);
}
if (j - x >= chunks[i].length) {
chunks[i][j] = null;
}
else {
chunks[i][j] = chunks[i][j - x];
}
}
}
}
if (y > 0) {
for (var i = 0; i < chunks.length; i++) {
if (i + y >= chunks.length) {
for (var j = 0; j < chunks.length; j++) {
if(i - y < 0 && chunks[i][j]) {
unloadChunk(i, j);
}
chunks[i][j] = null;
}
}
else {
for (var j = 0; j < chunks.length; j++) {
if(i - y < 0 && chunks[i][j]) {
unloadChunk(i, j);
}
chunks[i][j] = chunks[i + y][j];
}
}
}
}
else if (y < 0) {
for (var i = chunks.length - 1; i >= 0; i--) {
if (i + y < 0) {
for (var j = 0; j < chunks.length; j++) {
if(i - y > chunks.length - 1 && chunks[i][j]) {
unloadChunk(i, j);
}
chunks[i][j] = null;
}
}
else {
for (var j = 0; j < chunks.length; j++) {
if(i - y > chunks.length - 1 && chunks[i][j]) {
unloadChunk(i, j);
}
chunks[i][j] = chunks[i + y][j];
}
}
}
}
}
If you're having trouble understanding exactly what I want the shift function to do, take a look at this fiddle and look at the html output.
My attempt at creating the shift function works, but it has 10 for loops. My question was, is there a more efficient, less verbose way to do this?
This proposal uses
Array#forEach: visit each item
Array#map: return value for each item
Array#pop: removes and return last element
Array#push: adds one or more elements at the end
Array#shift: removes and return first element
Array#unshift: adds one or more elements at the beginning
For better visibillity, I replaced the null value with 1000, 2000, 3000 and 4000.
function shift(x, y) {
while (x > 0) {
chunks.forEach(function (a) {
a.pop();
a.unshift(1000);
});
x--;
}
while (x < 0) {
chunks.forEach(function (a) {
a.shift();
a.push(2000);
});
x++;
}
while (y > 0) {
chunks.unshift(chunks.pop().map(function () { return 3000; }));
y--;
}
while (y < 0) {
chunks.push(chunks.shift().map(function () { return 4000; }));
y++;
}
}
function print(msg) {
document.body.innerHTML += '<p>' + msg + '</p>';
}
function printarr(arr) {
for (var i = 0; i < arr.length; i++) {
print(JSON.stringify(arr[i]))
}
}
var chunks = [[5, 3, 1], [9, 2, 5], [2, 3, 7]];
print("chunks: " + JSON.stringify(chunks));
shift(1, 0);
print("shifting right 1. chunks: "); printarr(chunks);
shift(-1, 0);
print("shifting left 1. chunks: "); printarr(chunks);
shift(0, 1);
print("shifting up 1. chunks: "); printarr(chunks);
shift(0, -1);
print("shifting down 1. chunks: "); printarr(chunks);
You can use pop(), push(), shift(), unshift() Array methods
var chunks = [
[5, 3, 1],
[9, 2, 5],
[2, 3, 7]
];
function shiftDown(){
chuck.pop();
chuck.unshift(Array(3));
}
function shiftUp(){
chuck.shift();
chuck.push(Array(3));
}
function shiftRight(){
chuck.foreach(function(v){
v.pop();
v.unshift(null);
})
}
function shiftLeft(){
chuck.foreach(function(v){
v.shift();
v.push(null);
})
}
If interpret Question correctly, you can use Array.prototype.forEach(), Array.prototype.splice()
var chunks = [[5, 3, 1], [9, 2, 5], [2, 3, 7]];
// `x`: Index at which to start changing array
// `y`: An integer indicating the number of old array elements to remove
function shift(arr, x, y, replacement) {
arr.forEach(function(curr, index) {
// The elements to add to the array, beginning at the start index.
// start index: `x`
curr.splice(x, y, replacement)
});
return arr
}
// e.g.,
shift(chunks, -1, 1, null);
console.log(chunks);
it must call unloadChunk for elements that should be set to null
it's not a good idea to mutate an Array, while you're iterating over the same Array. So change unloadChunk() to not change chunks, but return the new value.
in my case I'm filling all the null values with new values.
then why do you bother to insert the null-values in the first place? why don't you just insert the new values?
//one-dimensional shift
function shift(arr, offset, callback){
var i, j, len = arr.length;
if(len && (offset |= 0)){
typeof callback === "function" || (callback = function(v){return v});
if(offset < 0){
for(i=-offset,j=0; i<len;)arr[j++]=arr[i++];
while(j<len)arr[j]=callback(null,j++,arr);
}else if(offset){
for(i=len-offset,j=len; i>0;)arr[--j]=arr[--i];
for(i=0; i<j;++i)arr[i]=callback(null,i,arr);
}
}
return arr;
}
//two dimensional shift
function shift2d(matrix, offsetX, offsetY, callback){
var i, len = matrix.length, tmp, fn;
offsetY |= 0;
offsetX |= 0;
if(len && matrix[0].length && (offsetY || offsetX)){
typeof callback === "function" || (callback = function(v){return v});
fn = function(val,j){ return callback(null, [i,j], matrix) };
tmp = {length: matrix[0].length};
offsetY && shift(matrix, offsetY, function(){return tmp});
for(i = 0; i < len; ++i){
if(matrix[i] === tmp){
matrix[i] = Array.from(tmp,fn);
}else{
shift(matrix[i], offsetX, fn);
}
}
}
return matrix;
}
and the code:
var chunks = [[5, 3, 1], [9, 2, 5], [2, 3, 7]];
console.log(chunks);
console.log(shift2d(chunks, -1, 1));
console.log(shift2d(chunks, 1, -1, computeValue));
function computeValue(value, index, array){
console.log("value: %o index: %o, is chunks: %o", value, index, array === chunks);
//return the new value for this field
return JSON.stringify(index);
return Math.random();
//with ES6 array destructuring:
var [row,col] = index;
return row*3 + col;
}
shift() and shift2d() expect as the last argument an (optional) callback function that returns the new values.
For consistency reasons, I pass the same argument as Array.map and the others
value: always null, only there for consistency reasons
index: the "index" that is accessed. For shift2d this is an array of indices (take a look at array destructuring)
array: the current array/matrix. Don't mess around with this while it gets changed. The main reason to pass this argument, is to check wich Array you're currently processing.

Pair of elements from a specified array whose sum equals a specific target number

I am in mid of my JavaScript session. Find this code in my coding exercise. I understand the logic but I didn't get this map[nums[x]] condition.
function twoSum(nums, target_num) {
var map = [];
var indexnum = [];
for (var x = 0; x < nums.length; x++)
{
if (map[nums[x]] != null)
// what they meant by map[nums[x]]
{
index = map[nums[x]];
indexnum[0] = index+1;
indexnum[1] = x+1;
break;
}
else
{
map[target_num - nums[x]] = x;
}
}
return indexnum;
}
console.log(twoSum([10,20,10,40,50,60,70],50));
I am trying to get the Pair of elements from a specified array whose sum equals a specific target number. I have written below code.
function arraypair(array,sum){
for (i = 0;i < array.length;i++) {
var first = array[i];
for (j = i + 1;j < array.length;j++) {
var second = array[j];
if ((first + second) == sum) {
alert('First: ' + first + ' Second ' + second + ' SUM ' + sum);
console.log('First: ' + first + ' Second ' + second);
}
}
}
}
var a = [2, 4, 3, 5, 6, -2, 4, 7, 8, 9];
arraypair(a,7);
Is there any optimized way than above two solutions? Can some one explain the first solution what exactly map[nums[x]] this condition points to?
Using HashMap approach using time complexity approx O(n),below is the following code:
let twoSum = (array, sum) => {
let hashMap = {},
results = []
for (let i = 0; i < array.length; i++){
if (hashMap[array[i]]){
results.push([hashMap[array[i]], array[i]])
}else{
hashMap[sum - array[i]] = array[i];
}
}
return results;
}
console.log(twoSum([10,20,10,40,50,60,70,30],50));
result:
{[10, 40],[20, 30]}
I think the code is self explanatory ,even if you want help to understand it,let me know.I will be happy enough to for its explanation.
Hope it helps..
that map value you're seeing is a lookup table and that twoSum method has implemented what's called Dynamic Programming
In Dynamic Programming, you store values of your computations which you can re-use later on to find the solution.
Lets investigate how it works to better understand it:
twoSum([10,20,40,50,60,70], 50)
//I removed one of the duplicate 10s to make the example simpler
In iteration 0:
value is 10. Our target number is 50. When I see the number 10 in index 0, I make a note that if I ever find a 40 (50 - 10 = 40) in this list, then I can find its pair in index 0.
So in our map, 40 points to 0.
In iteration 2:
value is 40. I look at map my map to see I previously found a pair for 40.
map[nums[x]] (which is the same as map[40]) will return 0.
That means I have a pair for 40 at index 0.
0 and 2 make a pair.
Does that make any sense now?
Unlike in your solution where you have 2 nested loops, you can store previously computed values. This will save you processing time, but waste more space in the memory (because the lookup table will be needing the memory)
Also since you're writing this in javascript, your map can be an object instead of an array. It'll also make debugging a lot easier ;)
function twoSum(arr, S) {
const sum = [];
for(let i = 0; i< arr.length; i++) {
for(let j = i+1; j < arr.length; j++) {
if(S == arr[i] + arr[j]) sum.push([arr[i],arr[j]])
}
}
return sum
}
Brute Force not best way to solve but it works.
Please try the below code. It will give you all the unique pairs whose sum will be equal to the targetSum. It performs the binary search so will be better in performance. The time complexity of this solution is O(NLogN)
((arr,targetSum) => {
if ((arr && arr.length === 0) || targetSum === undefined) {
return false;
} else {
for (let x = 0; x <=arr.length -1; x++) {
let partnerInPair = targetSum - arr[x];
let start = x+1;
let end = (arr.length) - 2;
while(start <= end) {
let mid = parseInt(((start + end)/2));
if (arr[mid] === partnerInPair) {
console.log(`Pairs are ${arr[x]} and ${arr[mid]} `);
break;
} else if(partnerInPair < arr[mid]) {
end = mid - 1;
} else if(partnerInPair > arr[mid]) {
start = mid + 1;
}
}
};
};
})([0,1,2,3,4,5,6,7,8,9], 10)
function twoSum(arr, target) {
let res = [];
let indexes = [];
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (target === arr[i] + arr[j] && !indexes.includes(i) && !indexes.includes(j)) {
res.push([arr[i], arr[j]]);
indexes.push(i);
indexes.push(j);
}
}
}
return res;
}
console.log('Result - ',
twoSum([1,2,3,4,5,6,6,6,6,6,6,6,6,6,7,8,9,10], 12)
);
Brute force.
const findTwoNum = ((arr, value) => {
let result = [];
for(let i= 0; i< arr.length-1; i++) {
if(arr[i] > value) {
continue;
}
if(arr.includes(value-arr[i])) {
result.push(arr[i]);
result.push(value-arr[i]);
break;;
}
}
return result;
});
let arr = [20,10,40,50,60,70,30];
const value = 120;
console.log(findTwoNum(arr, value));
OUTPUT : Array [50, 70]
function twoSum(arr){
let constant = 17;
for(let i=0;i<arr.length-2;i++){
for(let j=i+1;j<arr.length;j++){
if(arr[i]+arr[j] === constant){
console.log(arr[i],arr[j]);
}
}
}
}
let myArr = [2, 4, 3, 5, 7, 8, 9];
function getPair(arr, targetNum) {
for (let i = 0; i < arr.length; i++) {
let cNum = arr[i]; //my current number
for (let j = i; j < arr.length; j++) {
if (cNum !== arr[j] && cNum + arr[j] === targetNum) {
let pair = {};
pair.key1 = cNum;
pair.key2 = arr[j];
console.log(pair);
}
}
}
}
getPair(myArr, 7)
let sumArray = (arr,target) => {
let ar = []
arr.forEach((element,index) => {
console.log(index);
arr.forEach((element2, index2) => {
if( (index2 > index) && (element + element2 == target)){
ar.push({element, element2})
}
});
});
return ar
}
console.log(sumArray([8, 7, 2, 5, 3, 1],10))
Use {} hash object for storing and fast lookups.
Use simple for loop so you can return as soon as you find the right combo; array methods like .forEach() have to finish iterating no matter what.
And make sure you handle edges cases like this: twoSum([1,2,3,4], 8)---that should return undefined, but if you don't check for !== i (see below), you would erroneously return [4,4]. Think about why that is...
function twoSum(nums, target) {
const lookup = {};
for (let i = 0; i < nums.length; i++) {
const n = nums[i];
if (lookup[n] === undefined) {//lookup n; seen it before?
lookup[n] = i; //no, so add to dictionary with index as value
}
//seen target - n before? if so, is it different than n?
if (lookup[target - n] !== undefined && lookup[target - n] !== i) {
return [target - n, n];//yep, so we return our answer!
}
}
return undefined;//didn't find anything, so return undefined
}
We can fix this with simple JS object as well.
const twoSum = (arr, num) => {
let obj = {};
let res = [];
arr.map(item => {
let com = num - item;
if (obj[com]) {
res.push([obj[com], item]);
} else {
obj[item] = item;
}
});
return res;
};
console.log(twoSum([2, 3, 2, 5, 4, 9, 6, 8, 8, 7], 10));
// Output: [ [ 4, 6 ], [ 2, 8 ], [ 2, 8 ], [ 3, 7 ] ]
Solution In Java
Solution 1
public static int[] twoNumberSum(int[] array, int targetSum) {
for(int i=0;i<array.length;i++){
int first=array[i];
for(int j=i+1;j<array.length;j++){
int second=array[j];
if(first+second==targetSum){
return new int[]{first,second};
}
}
}
return new int[0];
}
Solution 2
public static int[] twoNumberSum(int[] array, int targetSum) {
Set<Integer> nums=new HashSet<Integer>();
for(int num:array){
int pmatch=targetSum-num;
if(nums.contains(pmatch)){
return new int[]{pmatch,num};
}else{
nums.add(num);
}
}
return new int[0];
}
Solution 3
public static int[] twoNumberSum(int[] array, int targetSum) {
Arrays.sort(array);
int left=0;
int right=array.length-1;
while(left<right){
int currentSum=array[left]+array[right];
if(currentSum==targetSum){
return new int[]{array[left],array[right]};
}else if(currentSum<targetSum){
left++;
}else if(currentSum>targetSum){
right--;
}
}
return new int[0];
}
function findPairOfNumbers(arr, targetSum) {
var low = 0, high = arr.length - 1, sum, result = [];
while(low < high) {
sum = arr[low] + arr[high];
if(sum < targetSum)
low++;
else if(sum > targetSum)
high--;
else if(sum === targetSum) {
result.push({val1: arr[low], val2: arr[high]});
high--;
}
}
return (result || false);
}
var pairs = findPairOfNumbers([1,2,3,4,4,5], 8);
if(pairs.length) {
console.log(pairs);
} else {
console.log("No pair of numbers found that sums to " + 8);
}
Simple Solution would be in javascript is:
var arr = [7,5,10,-5,9,14,45,77,5,3];
var arrLen = arr.length;
var sum = 15;
function findSumOfArrayInGiven (arr, arrLen, sum){
var left = 0;
var right = arrLen - 1;
// Sort Array in Ascending Order
arr = arr.sort(function(a, b) {
return a - b;
})
// Iterate Over
while(left < right){
if(arr[left] + arr[right] === sum){
return {
res : true,
matchNum: arr[left] + ' + ' + arr[right]
};
}else if(arr[left] + arr[right] < sum){
left++;
}else{
right--;
}
}
return 0;
}
var resp = findSumOfArrayInGiven (arr, arrLen, sum);
// Display Desired output
if(resp.res === true){
console.log('Matching Numbers are: ' + resp.matchNum +' = '+ sum);
}else{
console.log('There are no matching numbers of given sum');
}
Runtime test JSBin: https://jsbin.com/vuhitudebi/edit?js,console
Runtime test JSFiddle: https://jsfiddle.net/arbaazshaikh919/de0amjxt/4/
function sumOfTwo(array, sumNumber) {
for (i of array) {
for (j of array) {
if (i + j === sumNumber) {
console.log([i, j])
}
}
}
}
sumOfTwo([1, 2, 3], 4)
function twoSum(args , total) {
let obj = [];
let a = args.length;
for(let i = 0 ; i < a ; i++){
for(let j = 0; j < a ; j++){
if(args[i] + args[j] == total) {
obj.push([args[i] , args[j]])
}
}
}
console.log(obj)}
twoSum([10,20,10,40,50,60,70,30],60);
/* */

Categories

Resources