Checking which element is bigger when merging two arrays? - javascript

I have two arrays that are the same length, for example var a = [5,2,6,2,7,5]; and var b = [2,3,7,4,3];.
I also have another array which is var c = [0,0,0,0,0];
How do I compare a and b to put the highest element into c which in this case should become [5,3,7,7,5];

ES6 single-line solution:
c = a.map((a, i) => a > b[i] ? a : b[i])

Array#map into a new array, and take the max of the current number from a, and the number with the same index from array b:
const a = [5, 2, 6, 2, 7];
const b = [2, 3, 7, 4, 3];
const c = a.map((num, i) => Math.max(num, b[i]));
console.log(c);

You would iterate through both arrays, doing the comparison at each step, and inserting the larger number:
Note: Even though you mention that you have equal length arrays, the two sample arrays you've given don't have the same length so my example uses similar equal-length arrays:
let a = [5, 2, 6, 2, 7]
let b = [2, 3, 7, 4, 3]
let c = [0, 0, 0, 0, 0]
// we can use a single loop index `i` since the arrays have same length
for (let i = 0; i < a.length; i++) {
// take the current number from a and from b
let numA = a[i]
let numB = b[i]
// determine larger of the two numbers
let largerNumber = numA > numB ? numA : numB
// add larger to array at current position
c[i] = largerNumber
}
console.log(c)
You can simplify your solution to be a simple map operation, as demonstrated by dhilt.

Just use a simple for loop:
var a = [2, 3, 7, 8];
var b = [3, 2, 5, 9];
var c = [0, 0, 0, 0];
for (var i = 0; i < c.length; i++) {
c[i] = a[i] > b[i] ? a[i] : b[i];
}
console.log("Result: "+c);

try this, below code takes two arrays and gives the result of max number
var array=[5,3,7,7,5];
var array2 = [5,6,4];
var array3=array.concat(array2);
var max=array3.sort((a,b)=>b-a)[0];
console.log("Result: " + max);

see example:
var a = [5,2,6,2,7,5];
var b = [2,3,7,4,3];
var c = [0,0,0,0,0];
for(var i in c){
c[i]=a[i]>b[i]?a[i]:b[i];
}
console.log('result:' + c);

Related

Best algorithm to perform alternate sorting of array using javascript?

The following was my interview question. But I couldn't crack it and even could not think how to get this done.
var arr = [1,4,5,8,3,2,6,9,7,10];
Expected output of alternate sorting:
[10,1,9,2,8,3,7,4,6,5]
What I have tried:
I tried slicing out the Math.max.apply(null,arr) and Math.min.apply(null,arr) alternatively to push into separate empty array. But It was told that the algorithm is not optimal.
I would sort the array, and then iterate it, picking values from the begining and the end (inloop calculated offsets), in each iteration. A final check to odd arrays would complete the process.
let a = [1, 4, 5, 8, 3, 2, 6, 9, 7, 10];
a.sort((a, b) => a - b);
let b =[];
let l = a.length-1; // micro optimization
let L = l/2; // micro optimization
for(var i=0; i<L; i++) b.push( a[l-i] ,a[i] );
if(a.length%2) b.push( a[i] ); // add last item in odd arrays
console.log(b);
Result :
b =  [10, 1, 9, 2, 8, 3, 7, 4, 6, 5]
Algorithm bennefits:
Avoiding alterations in the original array (through pop and shift), improves the performance considerably.
Precalculating l and L before the loop , prevents the need of being calculated repeatedly in each iteration.
A single conditional cheking at the end of the procces, to handle odd arrays, slightly improves the speed.
I've prepared some PERFORMANCE TESTS, with some of the proposed algorithms :
Original Array(10 items) and Big Array(1000 items)
Here is one way to do it:
var arr = [1, 4, 5, 8, 3, 2, 6, 9, 7, 10];
// Sort the source array
arr.sort((a, b) => a - b);
// This will be the final result
var result = [];
// Create two pointers
var a = 0,
b = arr.length - 1;
while (result.length < arr.length) {
// Push the elements from start and end to the result array
result.push(arr[b]);
// Avoid bug when array is odd lengthed
if (a !== b) {
result.push(arr[a]);
}
a++;
b--;
}
console.log(result);
The idea is to have two pointers (a and b) traversing the the sorted original array from both the directions and appending the elements in result.
If you assume that the array will be a set of sequential numbers (a good question to ask about the data) you can do this very quickly with no need to sort or mutate the original array(i.e O(n)):
var arr = [1, 4, 5, 8, 3, 2, 6, 9, 7, 10];
let a = arr.reduce((a, c, i) => {
a[c > arr.length >> 1 ? (arr.length - c) << 1 : (c << 1) - 1] = c
return a
}, [])
console.log(a)
Here's my answer, based off the intuition that you're taking from the front then the back repeatedly from the sorted array until you're empty. The trick is avoiding "max" and "min" which evaluate the entire array, and just sorting it once.
Many of the other answers will put an undefined into the array if the original array has an odd length. I would leave a comment on those but I do not have the reputation. This is why I bounds check twice per loop.
var arr = [1,4,5,8,3,2,6,9,7,10];
// Sort numerically (not lexicographically)
arr.sort((a, b) => a - b)
// The output array
var out = []
// Take from the front, then back until original array is empty
while (true) {
if (arr.length == 0) break
out.push(arr.pop())
if (arr.length == 0) break
out.push(arr.shift())
}
// Output answer
console.log(out)
My solution for readability / no hidden magic:
// Input
var arr = [1,4,5,8,3,2,6,9,7,10];
// Sort
var arr1 = arr.sort((a,b) => (a - b));
// Compose
var arr2 = [];
for (var i = 0; i < arr1.length; i++) {
arr2.push(arr1[(i % 2) === 0
? arr1.length-1-(i/2) // get from end half
: (i-1)/2 // get from begin half
])
}
// Output
console.log(arr2); // = [10, 1, 9, 2, 8, 3, 7, 4, 6, 5]
Their interview answer "that the algorithm is not optimal." is not unexpected ofcourse. I would inquire why they say that, and ask if its really benefitial to spend dollar time on dimes here. (or tens of dollars on cents, actually)
Alternative method with only one variable to increment:
var arr = [1, 4, 5, 8, 3, 2, 6, 9, 7, 10];
arr = arr.sort((a, b) => b - a);
var result = [];
var a = 0;
while (result.length < arr.length) {
result.push(arr[a]);
result.push(arr[arr.length - a - 1]);
a++;
}
console.log(result);
var a = [1,4,5,8,3,2,6,9,7,10];
var b = a.sort((a, b) => a - b);
var c = a.sort((a, b) => a - b).reverse();
var d = [];
let e = a.length-1;
let f = e/2;
for(let i=0; i<f; i++) d.push( b.pop(), c.pop() );
Replace b and c in the for loop with functions to test:
for(let i=0; i<f; i++) d.push( a.sort((a, b) => a - b).pop(), a.sort((a, b) => a - b).reverse().pop() );
sort the array and divide into two parts , now use reduce to put elements from the two arrays
//original array
var arr = [1, 4, 5, 8, 3, 2, 6, 9, 7, 10];
//sorting origina array in ascending order
var m = arr.sort(function(a, b) {
return a - b;
});
// diving the sorted array in two parts
let getFirstSet = m.splice(0, arr.length / 2);
// now m containleft over items after the splice
// again sorted it in descending order to avoid back looping
let getSecondSet = m.sort(function(a, b) {
return b - a;
});
//using reduce function
let newArray = getFirstSet.reduce(function(acc, curr, index) {
// pushing element from second array containing 10,9,8,7,6
acc.push(getSecondSet[index]);
// pushing element from first array containing 1,2,3,4,5
acc.push(getFirstSet[index]);
return acc;
}, []); // [] is the initial array where elements will be pushed
console.log(newArray)
Another alternative view ... should this funky sort be done in place, like .sort is?
let input = [1, 4, 5, 8, 3, 2, 6, 9, 7, 10];
input.sort((a, b) => b - a).every((n, i, a) => (a.splice((i * 2 + 1), 0, a.pop()), (i * 2) < a.length));
console.log(input);
Here is a quick solution, using ternary operators and modulo operator for toggling.
let arr = [1, 4, 5, 8, 3, 2, 6, 9, 7, 10];
let j = 0;
let k = arr.length - 1;
// sort array
arr.sort((a, b) => a - b);
let new_array = [];
for (let i in arr) {
new_array[i] = i % 2 == 0 ? arr[k--] : arr[j++];
}
// prints array
console.log(new_array);

how do I return new array every time I swap 2 values?

I'm working on some quiz and I have an array which is looking like this one: let a = [1, 3, 4, 2]
Now I'm wondering how do I create a loop that returns three new arrays with 2 swapped values i.e: (a[i] swapped with a[i+1])
I'm expecting to get 3 following arrays like below:
[3, 1, 4, 2]
[1, 4, 3, 2]
[1, 3, 2, 4]
What would be the approach creating the loop for this? I have been trying to loop through the array with map function and swapping values but found that I'm just confusing myself. Any constructive approach/solution with an explanation to this quiz would be appreciated.
map in a loop is all you should need
let a = [1, 3, 4, 2];
let r = [];
for (let i = 0; i < a.length - 1; i++) {
r[i] = a.map((v, index) => {
if(index == i) return a[i+1];
if(index == i + 1) return a[i];
return v;
});
}
console.log(r);
Using a simple for loop:
let a = [1, 3, 4, 2],
result = [];
for(let i = 0; i < a.length - 1; i++) { // notice that the loop ends at 1 before the last element
let copy = a.slice(0); // copy the array
let temp = copy[i]; // do the swaping
copy[i] = copy[i + 1];
copy[i + 1] = temp;
result.push(copy); // add the array to result
}
console.log(result);

Way to multiply each element of one array to PARTS of another?

I am newbie in programming and try to find out how to multiply
[1,1,0]
to
[4,9,7,2,1,6]
for the next result output
[4,9,7,2,0,0]
As you see I want to multiply each value of [1,1,0] array to each two of second array by shifting in them
[1..] * [4,9..] = [4,9]
[.1.] * [.7,2.] = [7,2]
[..0] * [..1,6] = [0,0]
As example in js i writed something like
var firstArray = [1,1,0];
var secondArray = [4,9,7,2,1,6];
var shift = secondArray / firstArray;
var startpos = 0;
var outArray = [];
for(i=0; i< firstArray.length; i++){
for(z=i; z< shift+i; z++){
outArray.push(firstArray[i] * secondArray[z]);
}
}
console.log(outArray);
It may be in python
You can abuse zip and list slicing:
a = [1, 1, 0]
b = [4, 9, 7, 2, 1, 6]
shift = len(b) // len(a) # use / in Python 2
li = []
for num_a, num_b1, num_b2 in zip(a, b[::shift], b[1::shift]):
li.extend([num_a * num_b1, num_a * num_b2])
print(li)
# [4, 9, 7, 2, 0, 0]
You can express this as a standard matrix multiplication by making your inputs 2D arrays.
I got the multiplication algorithm from here: https://stackoverflow.com/a/27205510/5710637
The calculation would now look like:
[1,0,0] [4,9] [4,9]
[0,1,0] * [7,2] = [7,2]
[0,0,0] [1,6] [0,0]
function multiplyMatrices(m1, m2) {
var result = [];
for (var i = 0; i < m1.length; i++) {
result[i] = [];
for (var j = 0; j < m2[0].length; j++) {
var sum = 0;
for (var k = 0; k < m1[0].length; k++) {
sum += m1[i][k] * m2[k][j];
}
result[i][j] = sum;
}
}
return result;
}
var in1 = [[1, 0, 0], [0, 1, 0], [0, 0, 0]];
var in2 = [[4, 9], [7, 2], [1, 6]]
console.log(multiplyMatrices(in1, in2))
In Javascript, you could use a more functional approach by using
Array#reduce for iterating the factors and returning a new array,
Array#concat for adding a part result after multiplying to the result set,
Array#slice, for getting only two elements of the values array,
Array#map for multiplying the part array with the given factor,
at least use an array as start value for reduce.
var factors = [1, 1, 0],
values = [4, 9, 7, 2, 1, 6],
result = factors.reduce(
(r, f, i) => r.concat(
values
.slice(i * 2, (i + 1) * 2)
.map(v => f * v)
),
[]
);
console.log(result);
You can do this:
Iterate over first array so that you get a number to multiply.
Then extract two elements from the target array and multiply, store the result in res array.
var mul = [1,1,0];
var target = [4,9,7,2,1,6];
var start = 0;
var res = [];
mul.forEach(function(v,i) {
var arr = target.slice(start, start+2);//from start index extract two
//elements
arr.forEach(function(val,i) {
res.push(v * val);
});
start += 2;
});
console.log(res);
You can use map() on array2 and use one var for incrementing index of array1 for each two elements and then multiply current element of array1 with element from array2 that has index as that var.
var a1 = [1,1,0];
var a2 = [4,9,7,2,1,6];
var j = 0;
var result = a2.map(function(e, i) {
if(i % 2 == 0 && i != 0) j++
return e * a1[j];
})
console.log(result)
In python you can use the following code. I'm assuming the length of the second array is larger and it is divisible by the length of the shorter array
def multiply(a, b):
x, y = len(a), len(b) # Find lengths
assert x % y == 0
n = x / y
result = [a[i] * b[i / n] for i in range(x)] # For each index in a, calculate the appropriate element in output by multiplying with relevant part
print result

Check if random numbers in array contains 5 of them in ascending order

Hello I want to check if 5 random numbers in array are ascending.
Example from this:
var array = [2, 5, 5, 4, 7, 3, 6];
to this:
array = [2,3,4,5,6];
and of course if higher sequence is possible:
array = [3,4,5,6,7];
Is there any shortcut for this kind of sorting in jQuery?
Thanks in advance.
var array = [2, 5, 5, 4, 7, 3, 6];
//first convert into an object literal for fast lookups.
var ao = {};
array.forEach(function (e) { ao[e] = true; });
//now loop all in array, and then loop again for 5
array.forEach(function (num) {
var count = 0, l;
for (l = 0; l < 5; l ++) {
if (ao[num + l]) count ++;
}
if (count === 5) {
//found, push into array just to show nice in console
var nums = [];
for (l = 0; l < 5; l ++) {
nums.push(num + l);
}
console.log(nums.join(','));
}
});
I think, this will do the trick:
get unique members
sort them
slice last 5
(or reverse, slice first 5, reverse) like I did, since your array could be less then 5.
If resulting array has length of 5 then you have a positive answer.
console.log($.unique([2,5,5,4,7,3,6]).sort().reverse().slice(0,5).reverse())
You might do as follows;
function checkStraight(a){
var s = [...new Set(a)].sort((a,b) => a-b);
return s.length >= 5 && s.reduce((p,c,i,a) => c - a[i-1] === 1 ? ++p
: p < 5 ? 1
: p , 1) > 4;
}
var array = [2, 5, 5, 4, 7, 3, 6, 9],
result = checkStraight(array);
console.log(result);

Sum specific array values depends on list of index value on other array

I have two arrays:
a = [12, 50, 2, 5, 6];
and
b = [0, 1, 3];
I want to sum those arrays value in array A with exact index value as array B so that would be 12+50+5 = 67. Kindly help me to do this in native javascript. I already tried searching but I can't find any luck. I found related article below, but I can't get the logic
indexOf method in an object array?
You can simply do as follows;
var arr = [12, 50, 2, 5, 6],
idx = [0, 1, 3],
sum = idx.map(i => arr[i])
.reduce((p,c) => p + c);
console.log(sum);
sumAIndiciesOfB = function (a, b) {
var runningSum = 0;
for(var i = 0; b.length; i++) {
runningSum += a[b[i]];
}
return runningSum;
};
logic explained:
loop through array b. For each value in b, look it up in array a (a[b[i]]) and then add it to runningSum. After looping through b you will have summed each index of a and the total will be in runningSum.
b contains the indices of a to sum, so loop over b, referencing a:
var sum=0, i;
for (i=0;i<b.length;i++)
{
sum = sum + a[b[i]];
}
// sum now equals your result
You could simply reduce array a and only add values if their index exists in array b.
a.reduce((prev, curr, index) => b.indexOf(index) >= 0 ? prev+curr : prev, 0)
The result is 12+50+5=67.
Like this:
function addByIndexes(numberArray, indexArray){
var n = 0;
for(var i=0,l=indexArray.length; i<l; i++){
n += numberArray[indexArray[i]];
}
return n;
}
console.log(addByIndexes([12, 50, 2, 5, 6], [0, 1, 3]));

Categories

Resources