Open In App

Set update() in Python to do union of n arrays

Last Updated : 21 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

We are given n arrays of any size which may have common elements, we need to combine all these arrays in such a way that each element should occurs only once and elements should be in sorted order? Examples:

Input : arr = [[1, 2, 2, 4, 3, 6],
              [5, 1, 3, 4],
              [9, 5, 7, 1],
              [2, 4, 1, 3]]
Output : [1, 2, 3, 4, 5, 6, 7, 9]

A simple solution for this problem is to create a empty hash and traverse each array one by one, this hash contains frequency of each element in list of arrays. Now traverse hash from start and print each index which has non zero value. Here we solve this problem in python very quickly using properties of Set() data structure and Update() method in python. 

How does Update() method works for set ?

anySet.update(iterable), this method does union of set named as anySet with any given iterable and it does not return any shallow copy of set like union() method, it updates the result into prefix set i.e; anySet.

Implementation:

Python3




# Function to combine n arrays
     
def combineAll(input):
         
    # cast first array as set and assign it
    # to variable named as result
    result = set(input[0])
     
    # now traverse remaining list of arrays
    # and take it's update with result variable
    for array in input[1:]:
        result.update(array)
     
    return list(result)
     
# Driver program
if __name__ == "__main__":
    input = [[1, 2, 2, 4, 3, 6],
            [5, 1, 3, 4],
            [9, 5, 7, 1],
            [2, 4, 1, 3]]
    print (combineAll(input))


Output

[1, 2, 3, 4, 5, 6, 7, 9]

Similar Reads

Union By Rank and Path Compression in Union-Find Algorithm
In the previous post, we introduced union find algorithm and used it to detect cycles in a graph. We used the following union() and find() operations for subsets. C/C++ Code // Naive implementation of find int find(int parent[], int i) { if (parent[i] == -1) return i; return find(parent, parent[i]); } // Naive implementation of union() void Union(i
15+ min read
Union-Find Algorithm | (Union By Rank and Find by Optimized Path Compression)
Check whether a given graph contains a cycle or not. Example: Input: Output: Graph contains Cycle. Input: Output: Graph does not contain Cycle. Prerequisites: Disjoint Set (Or Union-Find), Union By Rank and Path CompressionWe have already discussed union-find to detect cycle. Here we discuss find by path compression, where it is slightly modified t
13 min read
Mo's Algo with update and without update
Mo's Algorithm is a truly versatile algorithm that finds utility in a vast array of problems requiring querying an array or a list for a subarray. Now, let me tell you, this algorithm owes its efficiency to a very specific order of processing the queries, which significantly reduces the time required to access the array elements. How does Mo's Algo
15+ min read
Find the union of two NumPy arrays
To find union of two 1-dimensional arrays we can use function numpy.union1d() of Python Numpy library. It returns unique, sorted array with values that are in either of the two input arrays. Syntax: numpy.union1d(array1, array2) Note The arrays given in input are flattened if they are not 1-dimensional. Let's see examples of how to find union of tw
2 min read
Find Union and Intersection of two unsorted arrays
Given two unsorted arrays that represent two sets (elements in every array are distinct), find the union and intersection of two arrays. Example: arr1[] = {7, 1, 5, 2, 3, 6} arr2[] = {3, 8, 6, 20, 7} Then your program should print Union as {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6, 7}. Note that the elements of union and intersection can
15+ min read
Union and Intersection of two sorted arrays
Given two sorted arrays, find their union and intersection. Example: Input: arr1[] = {1, 3, 4, 5, 7} arr2[] = {2, 3, 5, 6} Output: Union : {1, 2, 3, 4, 5, 6, 7} Intersection : {3, 5} Input: arr1[] = {2, 5, 6} arr2[] = {4, 6, 8, 10} Output: Union : {2, 4, 5, 6, 8, 10} Intersection : {6} Union of Two-Sorted Arrays using SetsThe idea of the approach i
15+ min read
Disjoint Set Union on trees | Set 2
Given a tree, and the cost of a subtree is defined as |S|*AND(S) where |S| is the size of the subtree and AND(S) is bitwise AND of all indices of nodes from the subtree, task is to find maximum cost of possible subtree. Prerequisite : Disjoint Set Union Examples: Input : Number of nodes = 4 Edges = (1, 2), (3, 4), (1, 3) Output : Maximum cost = 4 E
14 min read
Python set operations (union, intersection, difference and symmetric difference)
This article demonstrates different operations on Python sets. Examples: Input : A = {0, 2, 4, 6, 8} B = {1, 2, 3, 4, 5} Output : Union : [0, 1, 2, 3, 4, 5, 6, 8] Intersection : [2, 4] Difference : [8, 0, 6] Symmetric difference : [0, 1, 3, 5, 6, 8] In Python, below quick operands can be used for different operations. | for union. & for interse
1 min read
Increasing Decreasing Update Queries (Akku and Arrays)
Akku has solved many problems, she is a genius. One day her friend gave her an Array of sizes n and asked her to perform some queries of the following type: Each query consists of three integers 1 A B: Update the Array at index A by value B2 A B: if the subarray from index A to B (both inclusive) is Both increasing(Non-decreasing) and decreasing(No
15+ min read
Disjoint Set Union on Trees
Given a tree and weights of nodes. Weights are non-negative integers. Task is to find maximum size of a subtree of a given tree such that all nodes are even in weights. Prerequisite : Disjoint Set Union Examples : Input : Number of nodes = 7 Weights of nodes = 1 2 6 4 2 0 3 Edges = (1, 2), (1, 3), (2, 4), (2, 5), (4, 6), (6, 7) Output : Maximum siz
11 min read
Article Tags :
Practice Tags :