> For the complete documentation index, see [llms.txt](https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/07-array-problems-i/discord-homework-list/07-union-of-two-sorted-arrays.md).

# 07 Union of two Sorted Arrays

{% embed url="<https://practice.geeksforgeeks.org/problems/union-of-two-arrays3538/1>" %}
GeeksFor Geeks QUesiton
{% endembed %}

### Using Set

```cpp
int doUnion(int a[], int n, int b[], int m)  {
    set<int> s(a, a + n);
    for(int i = 0; i < m; i++)
        s.insert(b[i]);
        
    return s.size();
}
```

Sorting takes $$O(n \log mn)$$

Inserting n elements in set takes

$$\log(1) + \log(2) + ... + \log(n)$$ time&#x20;

$$=\log(1\times2\times ... \times n)$$$$=\log(n!)$$

So, for inserting n and m elements it takes&#x20;

$$\log(n!\times m!)$$ time

So, Time Complexity = $$O(\log(n!\times m!))$$

Space Complexity = $$O(m + n)$$​

### Using Look Up Table

```cpp
int doUnion(int a[], int n, int b[], int m)  {
    // find the largest element from both the arrays
    int maxElement = INT_MIN;
    for(int i = 0; i < n; i++)
        maxElement = max(maxElement, a[i]);
    for(int i = 0; i < m; i++)
        maxElement = max(maxElement, b[i]);
        
    // create a lookup table as big as the largest element
    vector<bool> present(maxElement + 1, false);
    
    // result
    int count = 0;
    
    // for every element in first array
    for(int i = 0; i < n; i++)
        // if it has not been encountered yet
        if(!present[a[i]]) {
            // mark it as seen
            present[a[i]] = true;
            // count it once
            count++;
        } // any subsequent encounters will not be counted
        
    // for every element in second array
    for(int i = 0; i < m; i++)
        // if it has not been encoutered yet.
        // This includes encounters from 1st array
        if(!present[b[i]]) {
            // mark it as seen
            present[b[i]] = true;
            // count it once.
            count++;
        } // any subsequent encouters will not be counted
        
    // final result in count
    return count;
}
```

Time Complexity: $$O(m + n)$$

Space complexity: $$O(max(a, b))$$


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/07-array-problems-i/discord-homework-list/07-union-of-two-sorted-arrays.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
