SINTERSEC - Editorial

Problem Link - Common Unique Elements

Problem Statement

Given two vectors of integers, find the common unique elements which are present in both the vectors.

Approach

The idea is to find unique integers that appear in both input lists. To achieve this, first store all unique elements of the first list in a set, as sets automatically handle duplicates. Then, iterate through the second list and check if each element exists in the set of the first list. If an element is found, add it to another set that keeps track of the common unique elements. At the end, output all the elements from this second set, as it contains only the unique common elements. This logic ensures that the solution is efficient and avoids duplicate checks.

Time Complexity

The time complexity is O(n + m), where n and m are the sizes of the two lists, due to the operations of inserting and checking elements in sets.

Space Complexity

The space complexity is O(n + k), where n is the size of the first list and k is the number of unique common elements.