This is one of the easiste explanation and more intuitive code to understand 🙂 Cheers! Here's the code in JS class OptimalSolution { unionArray(nums1, nums2) { const n = nums1.length; //length of the array (nums1) const m = nums2.length; //length of the array (nums2) let i = 0; //Pointer 1 let j = 0; //Pointer 2 const union = []; while (i < n && j < m) { console.log(`nums1: index -> ${i} and value -> ${nums1[i]}`); console.log(`nums2: index -> ${j} and value -> ${nums2[j]}`); //If there are duplicate elements in the array1 then move the 'i' pointer while (i + 1 < n) { if (nums1[i + 1] === nums1[i]) { console.log(`nums1: index -> ${i} value -> ${nums1[i]} and index -> ${i + 1} value -> ${nums1[i + 1]} is same`); i++; console.log('******************************************************************************************************'); } else { break; } } //If there are duplicate elements in the array2 then move the 'j' pointer while (j + 1 < m) { if (nums2[j + 1] === nums2[j]) { console.log(`nums2: index -> ${j} value -> ${nums2[j]} and index -> ${j + 1} value -> ${nums2[j + 1]} is same`); j++; console.log('******************************************************************************************************'); } else { break; } } //Compare elements of both the array if (nums1[i] < nums2[j]) { console.log(`nums1: index -> ${i} value -> ${nums1[i]} is less than num2: index -> ${j} value -> ${nums2[j]}`); union.push(nums1[i]); i++; } else if (nums1[i] > nums2[j]) { console.log(`nums1: index -> ${i} value -> ${nums1[i]} is greater than num2: index -> ${j} value -> ${nums2[j]}`); union.push(nums2[j]); j++; } else if (nums1[i] === nums2[j]) { console.log(`nums1: index -> ${i} value -> ${nums1[i]} and num2: index -> ${j} value -> ${nums2[j]} is same`); union.push(nums1[i]); i++; j++; } console.log('union array: ', union); console.log('-----------------------------------------------------------------------------------------------------
'); } //Remaining elements of nums1 while (i < n) { while (i + 1 < n) { if (nums1[i + 1] === nums1[i]) { console.log(`nums1: index -> ${i} value -> ${nums1[i]} and index -> ${i + 1} value -> ${nums1[i + 1]} is same`); i++; } else { break; } console.log('******************************************************************************************************'); } union.push(nums1[i]); i++; console.log('-----------------------------------------------------------------------------------------------------
'); } while (j < m) { while (j + 1 < m) { if (nums2[j + 1] === nums2[j]) { console.log(`nums2: index -> ${j} value -> ${nums2[j]} and index -> ${j + 1} value -> ${nums2[j + 1]} is same`); j++; } else { break; } console.log('******************************************************************************************************'); } union.push(nums2[j]); j++; console.log('-----------------------------------------------------------------------------------------------------
'); } console.log('final union array: ', union); } }
This is one of the easiste explanation and more intuitive code to understand 🙂 Cheers!
Here's the code in JS
class OptimalSolution {
unionArray(nums1, nums2) {
const n = nums1.length; //length of the array (nums1)
const m = nums2.length; //length of the array (nums2)
let i = 0; //Pointer 1
let j = 0; //Pointer 2
const union = [];
while (i < n && j < m) {
console.log(`nums1: index -> ${i} and value -> ${nums1[i]}`);
console.log(`nums2: index -> ${j} and value -> ${nums2[j]}`);
//If there are duplicate elements in the array1 then move the 'i' pointer
while (i + 1 < n) {
if (nums1[i + 1] === nums1[i]) {
console.log(`nums1: index -> ${i} value -> ${nums1[i]} and index -> ${i + 1} value -> ${nums1[i + 1]} is same`);
i++;
console.log('******************************************************************************************************');
} else {
break;
}
}
//If there are duplicate elements in the array2 then move the 'j' pointer
while (j + 1 < m) {
if (nums2[j + 1] === nums2[j]) {
console.log(`nums2: index -> ${j} value -> ${nums2[j]} and index -> ${j + 1} value -> ${nums2[j + 1]} is same`);
j++;
console.log('******************************************************************************************************');
} else {
break;
}
}
//Compare elements of both the array
if (nums1[i] < nums2[j]) {
console.log(`nums1: index -> ${i} value -> ${nums1[i]} is less than num2: index -> ${j} value -> ${nums2[j]}`);
union.push(nums1[i]);
i++;
} else if (nums1[i] > nums2[j]) {
console.log(`nums1: index -> ${i} value -> ${nums1[i]} is greater than num2: index -> ${j} value -> ${nums2[j]}`);
union.push(nums2[j]);
j++;
} else if (nums1[i] === nums2[j]) {
console.log(`nums1: index -> ${i} value -> ${nums1[i]} and num2: index -> ${j} value -> ${nums2[j]} is same`);
union.push(nums1[i]);
i++;
j++;
}
console.log('union array: ', union);
console.log('-----------------------------------------------------------------------------------------------------
');
}
//Remaining elements of nums1
while (i < n) {
while (i + 1 < n) {
if (nums1[i + 1] === nums1[i]) {
console.log(`nums1: index -> ${i} value -> ${nums1[i]} and index -> ${i + 1} value -> ${nums1[i + 1]} is same`);
i++;
} else {
break;
}
console.log('******************************************************************************************************');
}
union.push(nums1[i]);
i++;
console.log('-----------------------------------------------------------------------------------------------------
');
}
while (j < m) {
while (j + 1 < m) {
if (nums2[j + 1] === nums2[j]) {
console.log(`nums2: index -> ${j} value -> ${nums2[j]} and index -> ${j + 1} value -> ${nums2[j + 1]} is same`);
j++;
} else {
break;
}
console.log('******************************************************************************************************');
}
union.push(nums2[j]);
j++;
console.log('-----------------------------------------------------------------------------------------------------
');
}
console.log('final union array: ', union);
}
}
@@deveshvyas3633 thank you for sharing code in js. 🥰😊🙌🏻
can i cheat
def union(arr1, arr2):
return list(set(arr1) | set(arr2))
Best question tha dimag hiladala..😂
mam striver dsa a2z sheet follow kar rahe h kya
beech beech mein, kuch apne questions bhi jo imp lgte hai :)
Thank you didi
Most welcome 😊
Great thanks
welcome :)
Hey aayushi Microsoft first day vlog upload kardo
sure, jaldi hi aayega :)
Mam please accept my linkdin request
Sunil, can you please send again, I'm not able to find, or can you send ur profile link here
@@AyushiSharmaDSA I also sent