Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Union of two sorted arrays
// Get the union of two sorted arrays. var arr1 = [1, 2, 4, 5]; var arr2 = [2, 3, 6, 7, 8]; var result = []; //output : [1, 2, 3, 4, 5, 6, 7, 8] var i = 0; var j = 0; while(i < arr1.length && j < arr2.length) { if (arr1[i] < arr2[j]) { //console.log(arr1[i]); result.push(arr1[i]); i++; } else if (arr2[j] < arr1[i]) { //console.log(arr2[j]); result.push(arr2[j]); j++; } else { //console.log(arr1[i]); result.push(arr1[1]); i++; j++; } } while(i < arr1.length) { //console.log(arr1[i]); result.push(arr1[i]); i++; } while(j < arr2.length) { //console.log(arr2[j]); result.push(arr2[j]); j++; } console.log(result);
run
|
edit
|
history
|
help
0
horsevalue
Add property to object
Union of two sorted arrays
Lowertoupper
Accept matching keys in object
Find the missing number in array
demo.js
JS - check 2 arrays, add 1 or 2, logical operations
fast function to reverse unsigned integer
RFID TEST