Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmSet update() in Python to do union of n arrays

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

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]

This article is contributed by Shashank Mishra (Gullu). If you like neveropen and would like to contribute, you can also write an article using write.neveropen.co.za or mail your article to review-team@neveropen.co.za. See your article appearing on the neveropen main page and help other Geeks.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments