Given two arrays of length N and M, print the merged array in ascending order containing only unique elements.
Example 1:
Input:
N = 2
a[] = {1, 8}
M = 2
b[] = {10, 11}
Output:
answer[] = {1, 8, 10, 11}
Explanation:
The answer[] array after merging both
the arrays and removing duplicates is
[1 8, 10, 11]
You have to return the size of the array
formed ( 4 in this case) and update the
answer array in the function mergeNsort().
 
Example 2:
Input:
N = 5
a[] = {7, 1, 5, 3, 9}
M = 6
b[]  = {8, 4, 3, 5, 2, 6}
Output:
answer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9} 
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function mergeNsort() which takes the array A[], B[] and its size N, M respectively as inputs and returns the size of this array and store the merged array sorted in ascending order in the answer[].
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ M ≤ 10^{5}
1 ≤ A_{i} ≤ 10^{5}
1 ≤ B_{i} ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: #User function Template for python3

class Solution:
    def mergeNsort(self, a, b, n, m, answer):
        # Write your code here


Solutions:
class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		z = set(a + b)
		answer[:] = list(sorted(set(z)))
		return len(z)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		list1 = []
		list1 = list(sorted(set(a + b)))
		for i in range(len(list1)):
			answer[i] = list1[i]
		return len(list1)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		answer.clear()
		answer.extend(sorted(list(set(a + b))))
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		hash = [0 for i in range(100001)]
		k = 0
		for i in range(0, n):
			hash[a[i]] += 1
		for i in range(0, m):
			hash[b[i]] += 1
		for i in range(1, 100001):
			if hash[i] > 0:
				answer[k] = i
				k += 1
		return k


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		a.sort()
		b.sort()
		i = 0
		j = 0
		ans = []
		while i < n and j < m:
			if a[i] < b[j]:
				ans.append(a[i])
				i += 1
			elif a[i] > b[j]:
				ans.append(b[j])
				j += 1
			else:
				ans.append(a[i])
				i += 1
				j += 1
		while i < n:
			ans.append(a[i])
			i += 1
		while j < m:
			ans.append(b[j])
			j += 1
		d = {}
		for i in ans:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		arr = []
		for i in d:
			arr.append(i)
		arr.sort()
		for i in range(len(arr)):
			answer[i] = arr[i]
		return len(arr)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		the_set = set()
		for i in a:
			the_set.add(i)
		for i in b:
			the_set.add(i)
		answer[:] = list(sorted(the_set))
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		res = list(set(a + b))
		res.sort()
		answer[:] = res
		return len(res)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		answer.clear()
		merge = set(a) | set(b)
		for item in merge:
			answer.append(item)
		answer.sort()
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		s = {}
		for i in a:
			if i in s:
				s[i] += 1
			else:
				s[i] = 1
		for j in b:
			if j in s:
				s[j] += 1
			else:
				s[j] = 1
		s = sorted(s)
		for i in range(len(s)):
			answer[i] = s[i]
		return len(s)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		k = list(set(a))
		p = list(set(b))
		k.sort()
		p.sort()
		c = k + p
		z = list(set(c))
		z.sort()
		answer[:] = z[:]
		return len(z)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		l = set(a + b)
		p = list(l)
		p.sort()
		for i in range(len(p)):
			answer[i] = p[i]
		return len(p)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		result = []
		x = set(a)
		y = set(b)
		for i in x:
			y.add(i)
		k = sorted(y)
		for i in range(len(k)):
			answer[i] = k[i]
		return len(k)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		a.extend(b)
		a.sort()
		answer = sorted(set(a))
		for k in answer:
			print(k, end=' ')
		return 0


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		answer.clear()
		answer = a + b
		answer = sorted(set(answer))
		for i in range(len(answer)):
			print(answer[i], end=' ')
		return 0


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		c = a + b
		c = list(set(c))
		c.sort()
		for i in range(0, len(c)):
			answer[i] = c[i]
		return len(c)


import heapq

class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		set_answer = set(a + b)
		heap = list(set_answer)
		heapq.heapify(heap)
		i = 0
		while heap:
			answer[i] = heapq.heappop(heap)
			i += 1
		return i


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		arr_hldr = a + b
		srt_unq = list(set(arr_hldr))
		srt_unq.sort()
		answer.clear()
		for i in range(0, len(srt_unq)):
			answer.append(srt_unq[i])
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		answer.clear()
		l1 = []
		l1.extend(list(set(a)))
		l1.extend(list(set(b)))
		answer.extend(list(set(l1)))
		answer.sort()
		return len(answer)


class Solution:

	def mergesort(self, arr):
		if len(arr) > 1:
			mid = len(arr) // 2
			L = arr[:mid]
			R = arr[mid:]
			self.mergesort(L)
			self.mergesort(R)
			i = j = k = 0
			while i < len(L) and j < len(R):
				if L[i] <= R[j]:
					arr[k] = L[i]
					i += 1
				else:
					arr[k] = R[j]
					j += 1
				k += 1
			while i < len(L):
				arr[k] = L[i]
				k += 1
				i += 1
			while j < len(R):
				arr[k] = R[j]
				j += 1
				k += 1

	def mergeNsort(self, a, b, n, m, answer):
		answer[:] = set(a + b)
		self.mergesort(answer)
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		a = [*set(a)]
		b = [*set(b)]
		a.sort()
		b.sort()
		i = 0
		j = 0
		k = 0
		while i < len(a) and j < len(b):
			if a[i] < b[j]:
				answer[k] = a[i]
				i += 1
			elif a[i] == b[j]:
				answer[k] = a[i]
				i += 1
				j += 1
			else:
				answer[k] = b[j]
				j += 1
			k += 1
		while i < len(a):
			answer[k] = a[i]
			i += 1
			k += 1
		while j < len(b):
			answer[k] = b[j]
			j += 1
			k += 1
		return k


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		max1 = max(a)
		max2 = max(b)
		if max1 > max2:
			answer = [-1] * (max1 + 1)
		else:
			answer = [-1] * (max2 + 1)
		for i in a:
			answer[i] = 1
		for j in b:
			answer[j] = 1
		result = []
		for k in range(0, len(answer)):
			if answer[k] == 1:
				result.append(k)
		return result

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		m = int(input())
		b = [int(x) for x in input().strip().split()]
		ob = Solution()
		answer = [0 for i in range(n + m)]
		k = ob.mergeNsort(a, b, n, m, answer)
		for i in k:
			print(i, end=' ')
		print()
		T -= 1


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		a.sort()
		b.sort()
		len_a = len(a)
		len_b = len(b)
		i = j = k = 0
		while i < len_a and j < len_b:
			if a[i] < b[j]:
				answer[k] = a[i]
				i += 1
			else:
				answer[k] = b[j]
				j += 1
			k += 1
		while i < len_a:
			answer[k] = a[i]
			i += 1
			k += 1
		while j < len_b:
			answer[k] = b[j]
			j += 1
			k += 1
		l = 0
		for i in range(k):
			if answer[i] != answer[l]:
				l += 1
				answer[l] = answer[i]
		return l + 1


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		m1 = max(a)
		m2 = max(b)
		m = max(m1, m2)
		ar = [-1] * (m + 1)
		for j in a:
			ar[j] = j
		for j in b:
			ar[j] = j
		answer[:] = [j for j in ar if j != -1]
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		k = 0
		a.sort()
		b.sort()
		p = 0
		q = 0
		c = []
		while p < n and q < m:
			if a[p] < b[q]:
				c.append(a[p])
				p += 1
			elif a[p] > b[q]:
				c.append(b[q])
				q += 1
			else:
				c.append(a[p])
				p += 1
				q += 1
		while p < n:
			c.append(a[p])
			p += 1
		while q < m:
			c.append(b[q])
			q += 1
		k = len(c)
		e = 0
		for i in range(k - 1):
			if c[i] != c[i + 1]:
				answer[e] = c[i]
				e += 1
		answer[e] = c[k - 1]
		return e + 1


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		temp = a + b
		temp.sort()
		j = 0
		for i in range(m + n):
			if temp[i] != temp[i + 1]:
				answer[j] = temp[i]
				j += 1
			if i + 1 == m + n - 1:
				answer[j] = temp[i + 1]
				j += 1
				break
		return j


class Solution:

	def mergeNsort(self, a, b, n, m):
		for i in a:
			b.append(i)
		se = set(b)
		li = list(se)
		li.sort()
		return li


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		a.extend(b)
		res = list(set(a))
		res.sort()
		for i in range(len(res)):
			answer[i] = res[i]
		return len(res)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		ans = set(a)
		ans.update(set(b))
		ans = list(ans)
		answer[0] = 1
		ans.sort()
		for i in range(0, len(ans)):
			answer[i] = ans[i]
		return len(ans)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		for i in a:
			answer.append(i)
		for j in b:
			answer.append(j)
		del answer[:n + m + 5]
		answer[:] = list(sorted(set(a + b)))
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		ans = list(set(a + b))
		answer.clear()
		ans.sort()
		for i in ans:
			answer.append(i)
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		answer.clear()
		c = list(set(a + b))
		answer += c
		answer.sort()
		l = len(c)
		return l


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		answer.clear()
		k1 = a + b
		k1 = list(set(k1))
		answer += k1
		answer.sort()
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		c = a + b
		c = list(set(c))
		c.sort()
		answer[:] = c[:]
		return answer


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		x = a + b
		x = list(set(x))
		x.sort()
		answer[0:] = x
		return len(x)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		s = set(a + b)
		answer[:] = sorted(s)
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		a.sort()
		b.sort()
		result = a + b
		s = set(result)
		new_list = list(sorted(s))
		answer.clear()
		for element in new_list:
			answer.append(element)
		return len(answer)


class Solution:

	def mergeNsort(self, a, b, n, m, ans):
		ans[:] = list(set(a).union(set(b)))
		ans.sort()
		return len(ans)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		s1 = set(a).union(set(b))
		c = list(s1)
		c.sort()
		answer[:] = c[:]
		return len(c)


class Solution:

	def mergeNsort(self, a, b, n, m, answer):
		final = []
		final.extend(a)
		final.extend(b)
		final = sorted(list(set(final)))
		l = len(final)
		for i in range(l):
			answer[i] = final[i]
		return len(final)