Array - 22: Union of Two Unsorted Arrays
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
Superb explaination. I didnt get the idea of using set
Thanks. Glad it helped you.
Explanation was good🤝 easy to understand
Thanks for your nice feedback. Keep Watching.
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.
are sets or maps allowed in companies coding round?? Please answere?
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.
@@CodingSimplified Thank you!
How did it manage to print elements in sorted order?
But set does not preserve insertion order. May be linkedHashSet?
can we use hashset in interviews also?
Yes.
Dear Guruji,
does HashSet have any property of outputs the elements that we added in sorted order?
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
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
Please tell me, if maps or sets are allowed in coding round or not?
@@tusharnain6652 yes you can
@@sachinbisht4368 Thank you for reply!
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).
How to calculate the time complexity
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.
Runtime error
Could you please paste error. Ideally it should not throw Runtime error.
@@CodingSimplified is there any solution with O(m+n) time and O(1) space