Array - 22: Union of Two Unsorted Arrays

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • Code: thecodingsimpl...
    Solution:
    - Take a set & one by one put all values of first array & second array into it
    - Set only contains the unique elements, so it'll have the union of both two arrays
    - Time Complexity: O(n + m) for iterating the both array, where m - first array length, m - second array length
    Please check video for more info:
    This problem is similar to:
    how to do union of two unsorted array,
    union of two unsorted arrays,
    print union of two unsorted array,
    union,
    two unsorted array,
    array,
    java tutorial,
    coding simplified,
    java
    CHECK OUT CODING SIMPLIFIED
    / codingsimplified
    I started my CZcams channel, Coding Simplified, during Dec of 2015.
    Since then, I've published over 300+ videos. My account is Partner Verified.
    ★☆★ VIEW THE BLOG POST: ★☆★
    thecodingsimpli...
    ★☆★ SUBSCRIBE TO ME ON CZcams: ★☆★
    www.youtube.co...
    ★☆★ SEND EMAIL At: ★☆★
    Email: thecodingsimplified@gmail.com

Komentáře • 24

  • @surajgrandhi6742
    @surajgrandhi6742 Před 4 lety +3

    Superb explaination. I didnt get the idea of using set

  • @sumitghadi2576
    @sumitghadi2576 Před 4 lety +1

    Explanation was good🤝 easy to understand

  • @sherazdotnet
    @sherazdotnet Před 2 lety

    Looking at the code, these two while loops do the same. Why not call a function like Append and pass these array as parameters? Would make the code cleaner and less repetition.

  • @tusharnain6652
    @tusharnain6652 Před 2 lety +2

    are sets or maps allowed in companies coding round?? Please answere?

    • @CodingSimplified
      @CodingSimplified  Před 2 lety +1

      Yup, there is no issue. Map, hashset, hashmap all valid. In actual world, you won't start from int. You've to use all these.

    • @tusharnain6652
      @tusharnain6652 Před 2 lety

      @@CodingSimplified Thank you!

  • @piyushmathpal4244
    @piyushmathpal4244 Před 2 lety

    How did it manage to print elements in sorted order?

  • @swetamajhi7755
    @swetamajhi7755 Před 3 lety

    But set does not preserve insertion order. May be linkedHashSet?

  • @aadeshsharma0001
    @aadeshsharma0001 Před 3 lety +1

    can we use hashset in interviews also?

  • @ravitejapoosala9064
    @ravitejapoosala9064 Před 3 lety

    Dear Guruji,
    does HashSet have any property of outputs the elements that we added in sorted order?

    • @abhinavkumar9841
      @abhinavkumar9841 Před 26 dny

      you will have to print it there is a method called iterative through which you will have to iterate and print the elements of the hash

  • @sachinbisht4368
    @sachinbisht4368 Před 2 lety

    How the Time complexity is O(n+m) when we're also checking for every element if it is present in the array or not?
    I think it's going to be O(nm).
    Explain Please

    • @tusharnain6652
      @tusharnain6652 Před 2 lety

      Please tell me, if maps or sets are allowed in coding round or not?

    • @sachinbisht4368
      @sachinbisht4368 Před 2 lety +1

      @@tusharnain6652 yes you can

    • @tusharnain6652
      @tusharnain6652 Před 2 lety

      @@sachinbisht4368 Thank you for reply!

    • @sherazdotnet
      @sherazdotnet Před 2 lety

      n*m means that for every n, we go thru m. That's not what's happening here. When you go thru an array once, its O(n). Here we have two arrays and n is representing first input array and m is representing 2nd input array. We are going thru each array once and therefore its O(n) + O(m) ==> O(n+m).

  • @Telugubitlu
    @Telugubitlu Před 4 lety

    How to calculate the time complexity

    • @CodingSimplified
      @CodingSimplified  Před 4 lety

      Time complexity is respective of how many elements we're traversing. Like here, we're traversing elements of both array, so O(n + m), which is equal to linear case. Hope it helps you. In future, I'll create a generic video on how we can calculate Time complexity. Keep Watching. Thanks.

  • @rishmamitradhar3410
    @rishmamitradhar3410 Před 4 lety +1

    Runtime error

    • @CodingSimplified
      @CodingSimplified  Před 4 lety

      Could you please paste error. Ideally it should not throw Runtime error.

    • @karansharma8951
      @karansharma8951 Před 3 lety

      @@CodingSimplified is there any solution with O(m+n) time and O(1) space