Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions data_structures/arrays/union_intersection_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
A list that has the common distinct element from both arrays and
if there are repetitions of the element then only one occurrence
is considered, known as the union of both arrays.

A list that has common distinct elements from both arrays,
is the intersection of both arrays.


Example:
Input: a[] = {1, 2, 2, 3, 4, 8, 10}, b[] = {4, 6, 3, 1}
Output: {1, 2, 3, 4, 6, 8, 10}
Explanation: 1, 2, 3, 4, 6, 8 and 10 is the union of
elements present in array a[] and array b[].

Input: a[] = {1, 2, 2, 3, 4, 8, 10}, b[] = {4, 6, 3, 1}
Output: {1, 2, 3, 4}
Explanation: 1, 2, 3, and 4 are the intersection(common elements)
of elements present in array a[] and array b[].

https://www.geeksforgeeks.org/union-and-intersection-of-two-sorted-arrays-2/


"""

# Taking input lists from user

a = list(map(int, input("Enter elements of first list:").split()))
b = list(map(int, input("Enter elements of second list:").split()))
# Example Input
# Enter elements of first list: 3 4 6 4 4 6 7 41
# Enter elements of second list: 78 3 5 7 -1 9 2 -5


"""bitwise or (|) between the sets of both arrays
to find union and assign it into a variable A
in the form of lists.
bitwise and (&) between the sets of both arrays
to find intersection and assign it into a variable A
in the form of lists.
"""


# Mocking input by predefined lists
def mock_input():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: mock_input. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/union_intersection_array.py, please provide doctest for the function mock_input

a = [3, 4, 6, 4, 4, 6, 7, 41]
b = [78, 3, 5, 7, -1, 9, 2, -5]
return a, b


# Function to calculate union and intersection
def union_intersection(a, b):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: union_intersection. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/union_intersection_array.py, please provide doctest for the function union_intersection

Please provide type hint for the parameter: a

Please provide descriptive name for the parameter: a

Please provide type hint for the parameter: b

Please provide descriptive name for the parameter: b

# Calculating union and intersection
union = list(set(a) | set(b))
intersection = list(set(a) & set(b))

return union, intersection


# Example of mocking input and running the code
if __name__ == "__main__":
# Mocked input
a, b = mock_input()

# Calculate union and intersection
union, intersection = union_intersection(a, b)

print("Union of the arrays:", union)
print("Intersection of the arrays:", intersection)


A = list(set(a) | set(b))
B = list(set(a) & set(b))


print("Union of the arrays:", A)
print("intersection of the arrays:", B)

# Output
"""Union of the arrays: [2, 3, 4, 5, 6, 7, 41, 9, 78, -5, -1]
intersection of the arrays: [3, 7]
"""
Loading