Given two arrays a[] and b[] of size n and m respectively. The task is to find the number of elements in the union between these two arrays. 
Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in the union.
Note : Elements are not necessarily distinct.
Example 1:
Input:
5 3
1 2 3 4 5
1 2 3
Output: 
5
Explanation: 
1, 2, 3, 4 and 5 are the
elements which comes in the union set
of both arrays. So count is 5.
Example 2:
Input:
6 2 
85 25 1 32 54 6
85 2 
Output: 
7
Explanation: 
85, 25, 1, 32, 54, 6, and
2 are the elements which comes in the
union set of both arrays. So count is 7.
Your Task:
Complete doUnion funciton that takes a, n, b, m as parameters and returns the count of union elements of the two arrays. The printing is done by the driver code.
Constraints:
1 ≤ n, m ≤ 10^{5}
0 ≤ a[i], b[i] < 10^{5}
Expected Time Complexity : O(n+m)
Expected Auxilliary Space : O(n+m)

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3



class Solution:    

    #Function to return the count of number of elements in union of two arrays.

    def doUnion(self,a,n,b,m):

        

        #code here

Solutions:
class Solution:

	def doUnion(self, a, n, b, m):
		return len(set(a + b))


class Solution:

	def doUnion(self, a, n, b, m):
		new_array = set()
		for i in range(n):
			new_array.add(a[i])
		for i in range(m):
			new_array.add(b[i])
		return len(new_array)


class Solution:

	def doUnion(self, a, n, b, m):
		arr1 = set(a)
		arr2 = set(b)
		result = arr1 | arr2
		return len(result)


class Solution:

	def doUnion(self, a, n, b, m):
		res = a + b
		result = len(list(set(res)))
		return result


class Solution:

	def doUnion(self, a, n, b, m):
		for i in b:
			a.append(i)
		c = list(set(a))
		return len(c)


class Solution:

	def doUnion(self, a, n, b, m):
		output = set()
		mx = max(m, n)
		for i in range(mx):
			if i < n and a[i] not in output:
				output.add(a[i])
			if i < m and b[i] not in output:
				output.add(b[i])
		return len(output)


import numpy as np

class Solution:

	def doUnion(self, a, n, b, m):
		arr1 = set(a)
		arr2 = set(b)
		arr = list(arr1.union(arr2))
		return len(arr)


class Solution:

	def doUnion(self, a, n, b, m):
		d = {}
		ans = 0
		for i in a:
			if i in d:
				d[i] = d[i] + 1
			else:
				ans = ans + 1
				d[i] = 1
		for i in b:
			if i in d:
				d[i] = d[i] + 1
			else:
				ans = ans + 1
				d[i] = 1
		return ans


class Solution:

	def doUnion(self, a, n, b, m):
		union_set = set()
		for i in a:
			union_set.add(i)
		for i in b:
			union_set.add(i)
		return len(union_set)


class Solution:

	def doUnion(self, a, n, b, m):
		d = {}
		for item in a:
			if item in d:
				d[item] += 1
			else:
				d[item] = 1
		for item in b:
			if item in d:
				d[item] += 1
			else:
				d[item] = 1
		return len(d)


class Solution:

	def doUnion(self, a, n, b, m):
		dict = {}
		for i in a:
			dict[i] = 1
		for i in b:
			dict[i] = 1
		count = 0
		for (key, value) in dict.items():
			count += 1
		return count


class Solution:

	def doUnion(self, a, n, b, m):
		dic = {}
		for i in a:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		for j in b:
			if j in dic:
				dic[j] += 1
			else:
				dic[j] = 1
		return len(dic)


class Solution:

	def doUnion(self, a, n, b, m):
		x = set(a)
		y = set(b)
		z = x.union(y)
		return len(z)


import numpy as np

class Solution:

	def doUnion(self, a, n, b, m):
		arr1 = np.array(a)
		arr2 = np.array(b)
		un = np.union1d(arr1, arr2)
		return len(un)


class Solution:

	def doUnion(self, a, n, b, m):
		mp = {}
		for i in range(n):
			mp[a[i]] = i
		for i in range(m):
			mp[b[i]] = i
		return len(mp)


class Solution:

	def doUnion(self, a, n, b, m):
		k = []
		for i in range(0, len(a)):
			k.append(a[i])
		for i in range(0, len(b)):
			k.append(b[i])
		k = list(set(k))
		return len(k)


class Solution:

	def doUnion(self, a, n, b, m):
		st = set()
		for num in a:
			st.add(num)
		for num in b:
			st.add(num)
		return len(st)


class Solution:

	def doUnion(self, a, n, b, m):
		final = list(set(a) | set(b))
		lgth = len(final)
		return lgth


class Solution:

	def doUnion(self, a, n, b, m):
		se = set()
		for val in a:
			se.add(val)
		for val in b:
			se.add(val)
		return len(se)


class Solution:

	def doUnion(self, a, n, b, m):
		maxia = max(a)
		maxib = max(b)
		maxi = 0
		if maxia > maxib:
			maxi = maxia
		else:
			maxi = maxib
		freq = [0 for i in range(maxi + 1)]
		for i in a:
			freq[i] = 1
		for j in b:
			freq[j] = 1
		count = 0
		for k in freq:
			if k > 0:
				count += 1
		return count


class Solution:

	def doUnion(self, a, n, b, m):
		a = set(a)
		b = set(b)
		s = a.union(b)
		s = list(s)
		return len(s)


class Solution:

	def doUnion(self, a, n, b, m):
		m = {}
		for i in a:
			m[i] = 1 + m.get(i, 0)
		for i in b:
			m[i] = 1 + m.get(i, 0)
		return len(m)


class Solution:

	def doUnion(self, a, n, b, m):
		c = a + b
		d = set(c)
		return len(list(d))


class Solution:

	def doUnion(self, a, n, b, m):
		for i in b:
			a.append(i)
		res = set(a)
		return len(res)


import numpy as np

class Solution:

	def doUnion(self, a, n, b, m):
		arr = np.union1d(a, b)
		return len(arr)


class Solution:

	def doUnion(self, a, n, b, m):
		s = set()
		for i in a:
			s.add(i)
		for j in b:
			s.add(j)
		return len(s)


import numpy as np

class Solution:

	def doUnion(self, a, n, b, m):
		return len(set(a + b))


class Solution:

	def doUnion(self, a, n, b, m):
		dct = dict()
		for elem in a + b:
			dct[elem] = 1
		return len(dct.values())


class Solution:

	def doUnion(self, a, n, b, m):
		return len(set(a).union(set(b)))

def getMinMax(arr, n):
	if n % 2 == 0:
		mx = max(arr[0], arr[1])
		mn = min(arr[0], arr[1])
		i = 2
	else:
		mx = mn = arr[0]
		i = 1
	while i < n - 1:
		if arr[i] < arr[i + 1]:
			mx = max(mx, arr[i + 1])
			mn = min(mn, arr[i])
		else:
			mx = max(mx, arr[i])
			mn = min(mn, arr[i + 1])
		i += 2
	return (mn, mx)


class Solution:

	def doUnion(self, a, n, b, m):
		t1 = a + b
		s = set(t1)
		l = len(s)
		return l


class Solution:

	def doUnion(self, a, n, b, m):
		setA = set(a)
		setB = set(b)
		unionset = setA.union(setB)
		count = len(unionset)
		return count


class Solution:

	def doUnion(self, a, n, b, m):
		set_a = set(a)
		set_b = set(b)
		return len(set_a.union(set_b))


class Solution:

	def doUnion(self, a, n, b, m):
		aSet = set(a)
		bSet = set(b)
		aSet = aSet.union(bSet)
		return len(aSet)