5. Union of Two Sorted Arrays | Arrays - Easy | Learn DSA

Sdílet
Vložit
  • čas přidán 12. 09. 2024

Komentáře • 15

  • @deveshvyas3633
    @deveshvyas3633 Před 19 hodinami

    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);
    }
    }

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  Před 19 hodinami

      @@deveshvyas3633 thank you for sharing code in js. 🥰😊🙌🏻

  • @bigbang0047
    @bigbang0047 Před měsícem +2

    can i cheat
    def union(arr1, arr2):
    return list(set(arr1) | set(arr2))

  • @rohitvhanmore8742
    @rohitvhanmore8742 Před měsícem

    Best question tha dimag hiladala..😂

  • @GopalKumar-xu3iw
    @GopalKumar-xu3iw Před měsícem

    mam striver dsa a2z sheet follow kar rahe h kya

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  Před měsícem

      beech beech mein, kuch apne questions bhi jo imp lgte hai :)

  • @ankitraushan2195
    @ankitraushan2195 Před měsícem

    Thank you didi

  • @rishiraj2548
    @rishiraj2548 Před měsícem

    Great thanks

  • @TechtonicShiv
    @TechtonicShiv Před měsícem

    Hey aayushi Microsoft first day vlog upload kardo

  • @SunilSahu-sy5is
    @SunilSahu-sy5is Před měsícem

    Mam please accept my linkdin request

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  Před měsícem

      Sunil, can you please send again, I'm not able to find, or can you send ur profile link here

    • @ShivKumarMishra-x1m
      @ShivKumarMishra-x1m Před měsícem

      @@AyushiSharmaDSA I also sent