githubEdit

23. Union of Arrays with Duplicates

βœ… GFG solution to Union of Arrays with Duplicates: find all distinct elements from two arrays using efficient set-based or sorting techniques. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

You are given two arrays a[] and b[], return the Union of both the arrays in any order.

The Union of two arrays is a collection of all distinct elements present in either of the arrays. If an element appears more than once in one or both arrays, it should be included only once in the result.

Note: Elements of a[] and b[] are not necessarily distinct. You can return the Union in any order but the driver code will print the result in sorted order only.

πŸ“˜ Examples

Example 1

Input: a[] = [1, 2, 3, 2, 1], b[] = [3, 2, 2, 3, 3, 2]
Output: [1, 2, 3]
Explanation: Union set of both the arrays will be 1, 2 and 3.

Example 2

Input: a[] = [1, 2, 3], b[] = [4, 5, 6]
Output: [1, 2, 3, 4, 5, 6]
Explanation: Union set of both the arrays will be 1, 2, 3, 4, 5 and 6.

Example 3

πŸ”’ Constraints

  • $1 \le \text{a.size()}, \text{b.size()} \le 10^6$

  • $0 \le \text{a}[i], \text{b}[i] \le 10^5$

βœ… My Approach

The optimal solution uses Merge and Sort with Deduplication:

Merge + Sort + Unique

  1. Merge Arrays:

    • Combine both arrays into a single array.

    • This preserves all elements from both sources.

  2. Sort Combined Array:

    • Sort the merged array to group duplicate elements together.

    • Sorting brings identical elements adjacent to each other.

  3. Remove Duplicates:

    • Use two-pointer or set to eliminate consecutive duplicates.

    • In C++, use unique() algorithm which removes consecutive duplicates.

  4. Return Result:

    • The resulting array contains all distinct elements from both arrays.

Key Advantage: This approach is straightforward and leverages standard library functions for efficiency. While it requires sorting (O(n log n)), it's cache-friendly and has good practical performance.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O((n + m) log(n + m)), where n and m are the sizes of the two arrays. Merging takes O(n + m), sorting the combined array takes O((n + m) log(n + m)), and removing duplicates takes O(n + m). The sorting operation dominates the overall complexity.

  • Expected Auxiliary Space Complexity: O(n + m), as we create a new array to store the merged elements from both input arrays. The space required is proportional to the total number of elements.

πŸ§‘β€πŸ’» Code (C)

πŸ§‘β€πŸ’» Code (C++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Set-Based Approach

πŸ’‘ Algorithm Steps:

  1. Create an unordered set to store unique elements.

  2. Insert all elements from first array into the set.

  3. Insert all elements from second array into the set.

  4. Convert set back to vector and return.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n + m) - Linear insertion for both arrays

  • Auxiliary Space: πŸ’Ύ O(n + m) - Set storage in worst case

βœ… Why This Approach?

  • Optimal time complexity with linear performance

  • Automatic deduplication via set properties

  • Simple and clean implementation

πŸ“Š 3️⃣ TreeSet (Ordered Set) Approach

πŸ’‘ Algorithm Steps:

  1. Use ordered set (TreeSet in Java, set in C++) for automatic sorting.

  2. Insert all elements from both arrays.

  3. The set maintains elements in sorted order and removes duplicates.

  4. Convert to array/list and return.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O((n + m) log(n + m)) - Set insertion with log factor

  • Auxiliary Space: πŸ’Ύ O(n + m) - Set storage

βœ… Why This Approach?

  • Result is automatically sorted

  • Clean single data structure usage

  • Predictable performance characteristics

πŸ“Š 4️⃣ Frequency Map Approach

πŸ’‘ Algorithm Steps:

  1. Use hash map to track which elements have been seen.

  2. Iterate through first array, mark elements as seen.

  3. Iterate through second array, mark new elements.

  4. Collect all marked elements into result vector.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n + m) - Linear traversal with hash operations

  • Auxiliary Space: πŸ’Ύ O(n + m) - Map storage

βœ… Why This Approach?

  • Similar to set but shows map usage

  • Can be extended to track frequencies if needed

  • Explicit marking logic

πŸ“Š 5️⃣ Two-Pointer Merge (Sorted Arrays)

πŸ’‘ Algorithm Steps:

  1. First sort both arrays individually.

  2. Use two pointers to merge arrays while avoiding duplicates.

  3. Compare elements at both pointers and add smaller unique element.

  4. Handle remaining elements from either array.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n log n + m log m) - Sorting both arrays separately

  • Auxiliary Space: πŸ’Ύ O(1) - Excluding output array

βœ… Why This Approach?

  • Classic merge technique from merge sort

  • Space efficient if sorting in-place

  • Good for teaching two-pointer patterns

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🎯 Merge + Sort + Unique

🟑 O((n+m) log(n+m))

🟑 O(n + m)

πŸš€ STL optimized, cache-friendly

πŸ”§ Sorting overhead

πŸ” Unordered Set

🟒 O(n + m)

🟑 O(n + m)

⚑ Fastest average case

πŸ”§ Unordered output

πŸ“Š TreeSet (Ordered)

🟑 O((n+m) log(n+m))

🟑 O(n + m)

🎯 Sorted output automatically

🐌 Log factor per insertion

πŸ—‚οΈ Frequency Map

🟒 O(n + m)

🟑 O(n + m)

πŸ”§ Extensible for frequencies

πŸ”§ Similar to set

πŸ”„ Two-Pointer Merge

🟑 O(n log n + m log m)

🟒 O(1)*

πŸ’Ύ Space efficient

πŸ”§ Complex duplicate handling

*Excluding output array

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Fastest average performance

πŸ₯‡ Unordered Set

β˜…β˜…β˜…β˜…β˜…

πŸ“– Need sorted output

πŸ₯ˆ TreeSet (Ordered)

β˜…β˜…β˜…β˜…β˜…

πŸ’Ύ Memory constrained

πŸ₯‰ Two-Pointer Merge

β˜…β˜…β˜…β˜…β˜†

🎯 Competitive programming

πŸ… Merge + Sort + Unique

β˜…β˜…β˜…β˜…β˜…

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated