Given two lists V1 and V2 of sizes n and m respectively. Return the list of elements common to both the lists and return the list in sorted order. Duplicates may be there in the output list.
Example:
Input:
n = 5
v1[] = {3, 4, 2, 2, 4}
m = 4
v2[] = {3, 2, 2, 7}
Output:
2 2 3
Explanation:
The common elements in sorted order are {2 2 3}
Your Task:
This is a function problem. You need to complete the function common_element that takes both the lists as parameters and returns a list of common elements. 
Constraints:
1 ≤ n, m ≤ 10^{5}
1 ≤ V_{i} ≤ 10^{5}

Difficulty: EASY
Tags: Data structures
Skill types: Data structures

Starter code:
#User function Template for python3



class Solution:

    def common_element(self,v1,v2):

        #code here

Solutions:
class Solution:

	def common_element(self, v1, v2):
		res = []
		map = {}
		for i in v1:
			if i in map:
				map[i] += 1
			else:
				map[i] = 1
		v2.sort()
		for i in v2:
			if i in map and map[i] > 0:
				res.append(i)
				map[i] -= 1
		return res


class Solution:

	def common_element(self, v1, v2):
		v1.sort()
		v2.sort()
		i = 0
		j = 0
		ans = []
		while i < len(v1) and j < len(v2):
			if v1[i] == v2[j]:
				ans.append(v1[i])
				i += 1
				j += 1
			elif v1[i] < v2[j]:
				i += 1
			else:
				j += 1
		ans.sort()
		return ans


class Solution:

	def common_element(self, v1, v2):
		res = []
		v1Dict = {}
		v2Dict = {}
		for i in v1:
			v1Dict[i] = 1 + v1Dict.get(i, 0)
		for i in v2:
			v2Dict[i] = 1 + v2Dict.get(i, 0)
		for i in sorted(v1Dict):
			if i in v2Dict:
				c = [i] * min(v1Dict[i], v2Dict[i])
				res += c
		return res


class Solution:

	def common_element(self, v1, v2):
		res = []
		d = {}
		for num in v1:
			d[num] = d.get(num, 0) + 1
		for num in v2:
			if num in d and d[num] > 0:
				res.append(num)
				d[num] -= 1
		return sorted(res)


from heapq import heapify, heappush, heappop

class Solution:

	def common_element(self, v1, v2):
		hashmap = {}
		for i in v1:
			if i not in hashmap:
				hashmap[i] = 1
			else:
				hashmap[i] += 1
		result = []
		for j in v2:
			if j in hashmap and hashmap[j] > 0:
				hashmap[j] -= 1
				result.append(j)
		result.sort()
		return result


class Solution:

	def common_element(self, v1, v2):
		dict = {}
		c = []
		for i in v1:
			if i not in dict:
				dict[i] = 1
			else:
				dict[i] += 1
		for i in v2:
			if i in dict and dict[i] > 0:
				c.append(i)
				dict[i] -= 1
		c.sort()
		return c


from collections import Counter

class Solution:

	def common_element(self, v1, v2):
		v1 = Counter(v1)
		v2 = Counter(v2)
		lst = []
		for i in v1:
			if i in v2:
				for j in range(min(v1[i], v2[i])):
					lst.append(i)
		return sorted(lst)


class Solution:

	def common_element(self, v1, v2):
		mdict = {}
		res = []
		for x in v1:
			if x in mdict:
				mdict[x] += 1
			else:
				mdict[x] = 1
		v2.sort()
		for x in v2:
			if x in mdict and mdict[x] > 0:
				res.append(x)
				mdict[x] -= 1
		return res


from collections import defaultdict

class Solution:

	def common_element(self, v1, v2):
		v1.sort()
		v2.sort()
		l = []
		d1 = defaultdict(int)
		for i in v1:
			d1[i] += 1
		for j in v2:
			if j in d1 and d1[j] != 0:
				d1[j] -= 1
				l.append(j)
		return l


from collections import Counter

class Solution:

	def common_element(self, v1, v2):
		ans = []
		a = Counter(v1)
		v2.sort()
		for i in v2:
			if i in a and a[i] > 0:
				ans.append(i)
				a[i] -= 1
		return ans


class Solution:

	def common_element(self, v1, v2):
		(freq, l) = ({}, [])
		for i in v1:
			if i not in freq:
				freq.update({i: 1})
			else:
				freq[i] += 1
		for i in v2:
			if i in freq and freq[i] > 0:
				l.append(i)
				freq[i] -= 1
		return sorted(l)


class Solution:

	def common_element(self, v1, v2):
		v1 = sorted(v1)
		v2 = sorted(v2)
		d = {}
		d1 = {}
		l = []
		for i in v1:
			if i not in d:
				d[i] = 1
			else:
				d[i] = d[i] + 1
		for i in v2:
			if i not in d1:
				d1[i] = 1
			else:
				d1[i] = d1[i] + 1
		kkeys = list(d.keys())
		kkeys.sort()
		for i in kkeys:
			if i in d1:
				k = min(d[i], d1[i])
				l = l + [i] * k
		return l


class Solution:

	def common_element(self, v1, v2):
		v1.sort()
		v2.sort()
		b = []
		while v1 and v2:
			if v1[0] != v2[0]:
				if v1[0] < v2[0]:
					v1.pop(0)
				else:
					v2.pop(0)
			else:
				b.append(v1[0])
				v1.pop(0)
				v2.pop(0)
		return b


class Solution:

	def common_element(self, v1, v2):
		d = {}
		ans = []
		for i in v1:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in v2:
			if i in d and d[i] != 0:
				ans.append(i)
				d[i] -= 1
		ans.sort()
		return ans


from collections import defaultdict

class Solution:

	def common_element(self, v1, v2):
		ans = []
		m = {}
		for i in v1:
			if i in m:
				m[i] += 1
			else:
				m[i] = 1
		ans = []
		for i in v2:
			if i in m and m[i] > 0:
				ans.append(i)
				m[i] -= 1
		ans.sort()
		return ans


from collections import defaultdict

class Solution:

	def common_element(self, v1, v2):
		(d1, d2) = (defaultdict(int), defaultdict(int))
		for i in v1:
			d1[i] += 1
		for i in v2:
			d2[i] += 1
		ans = []
		for (key, val) in d1.items():
			if key in d2:
				second = d2[key]
				for i in range(min(second, val)):
					ans.append(key)
		return sorted(ans)


class Solution:

	def common_element(self, v1, v2):
		v1.sort()
		v2.sort()
		(i, j) = (0, 0)
		li = []
		(n1, n2) = (len(v1), len(v2))
		while i < n1 and j < n2:
			if v1[i] == v2[j]:
				li.append(v1[i])
				i += 1
				j += 1
			elif v1[i] < v2[j]:
				i += 1
			else:
				j += 1
		return li


class Solution:

	def common_element(self, v1, v2):
		v1.sort()
		v2.sort()
		res = []
		i = 0
		j = 0
		n = len(v1)
		m = len(v2)
		while i < n and j < m:
			if v1[i] == v2[j]:
				res.append(v1[i])
				i += 1
				j += 1
			elif v1[i] > v2[j]:
				j += 1
			else:
				i += 1
		return res


class Solution:

	def common_element(self, v1, v2):
		l = []
		d = {}
		for i in v1:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in v2:
			if i in d and d[i] > 0:
				l.append(i)
				d[i] -= 1
		return sorted(l)


class Solution:

	def common_element(self, v1, v2):
		map1 = {}
		map2 = {}
		for i in range(len(v1)):
			if v1[i] not in map1:
				map1[v1[i]] = 1
			else:
				map1[v1[i]] += 1
		for i in range(len(v2)):
			if v2[i] not in map2:
				map2[v2[i]] = 1
			else:
				map2[v2[i]] += 1
		new = []
		for i in map1:
			if i in map2:
				x = [i] * min(map1[i], map2[i])
				new.extend(x)
		new.sort()
		return new


from collections import Counter

class Solution:

	def common_element(self, v1, v2):
		c1 = Counter(v1)
		c2 = Counter(v2)
		m = len(v1)
		n = len(v2)

		def getCommEle(v, c1, c2):
			res = []
			for num in set(v):
				if c1[num] > 0 and c2[num] > 0:
					for i in range(min(c1[num], c2[num])):
						res.append(num)
			return sorted(res)
		if m <= n:
			return getCommEle(v1, c1, c2)
		else:
			return getCommEle(v2, c1, c2)


class Solution:

	def common_element(self, v1, v2):
		d = {}
		ans = []
		for val in v1:
			if val in d:
				d[val] = d[val] + 1
			else:
				d[val] = 1
		v2 = sorted(v2)
		for val in v2:
			if val in d and d[val] > 0:
				ans.append(val)
				d[val] = d[val] - 1
		return ans


class Solution:

	def common_element(self, v1, v2):
		ans = []
		v1 = sorted(v1)
		l1 = len(v1)
		v2 = sorted(v2)
		l2 = len(v2)
		i = 0
		j = 0
		while i < l1 and j < l2:
			if v1[i] == v2[j]:
				ans.append(v1[i])
				j = j + 1
				i = i + 1
			elif v1[i] > v2[j]:
				j = j + 1
			elif v1[i] < v2[j]:
				i = i + 1
		return ans


class Solution:

	def common_element(self, v1, v2):
		v1.sort()
		v2.sort()
		d1 = {}
		d2 = {}
		for i in v1:
			if i in d1:
				d1[i] += 1
			else:
				d1[i] = 1
		for i in v2:
			if i in d2:
				d2[i] += 1
			else:
				d2[i] = 1
		ans = []
		for i in d1:
			if i in d2:
				c = min(d1[i], d2[i])
				while c > 0:
					ans.append(i)
					c -= 1
		ans.sort()
		return ans


from collections import defaultdict

class Solution:

	def common_element(self, v1, v2):
		d = defaultdict(int)
		finalList = []
		for i in v1:
			d[i] += 1
		for i in v2:
			if d[i] != 0:
				finalList.append(i)
				d[i] -= 1
		finalList.sort()
		return finalList


import operator as op

class Solution:

	def common_element(self, v1, v2):
		ans = []
		i = j = 0
		v1 = sorted(v1)
		v2 = sorted(v2)
		while i < len(v1) and j < len(v2):
			if v1[i] == v2[j]:
				ans.append(v1[i])
				i += 1
				j += 1
			elif v1[i] > v2[j]:
				j += 1
			else:
				i += 1
		return ans


class Solution:

	def common_element(self, v1, v2):
		res = []
		a = sorted(v1)
		b = sorted(v2)
		i = 0
		j = 0
		while i < len(a) and j < len(b):
			if a[i] == b[j]:
				res.append(a[i])
				i = i + 1
				j = j + 1
			elif a[i] > b[j]:
				j = j + 1
			elif a[i] < b[j]:
				i = i + 1
		return sorted(res)


from collections import Counter

class Solution:

	def common_element(self, v1, v2):
		freq = Counter(v1)
		ans = list()
		for e in v2:
			if e in freq:
				freq[e] -= 1
				if freq[e] >= 0:
					ans.append(e)
		return sorted(ans)


class Solution:

	def common_element(self, v1, v2):
		result = []
		hash_v1 = {}
		hash_v2 = {}
		for i in range(len(v1)):
			if v1[i] in hash_v1:
				hash_v1[v1[i]] += 1
			else:
				hash_v1[v1[i]] = 1
		for i in range(len(v2)):
			if v2[i] in hash_v2:
				hash_v2[v2[i]] += 1
			else:
				hash_v2[v2[i]] = 1
		for i in hash_v1:
			if i in hash_v2:
				get_values_v1 = hash_v1[i]
				get_values_v2 = hash_v2[i]
				get_min = min(get_values_v1, get_values_v2)
				for j in range(get_min):
					result.append(i)
		result = sorted(result)


from collections import defaultdict

class Solution:

	def s(self):
		return 0

	def common_element(self, v1, v2):
		hash1 = defaultdict(self.s)
		(_, L1) = max((len(v1), v1), (len(v2), v2))
		(_, L2) = min((len(v1), v1), (len(v2), v2))
		for i in L2:
			hash1[i] += 1
		res = []
		for i in L1:
			if hash1[i] > 0:
				hash1[i] -= 1
				res.append(i)
		res.sort()
		return res


class Solution:

	def common_element(self, v1, v2):
		out = []
		my_hm = {}
		for i in v1:
			if i in my_hm:
				my_hm[i] += 1
			else:
				my_hm[i] = 1
		for j in v2:
			if j in my_hm and my_hm[j] != 0:
				out.append(j)
				my_hm[j] -= 1
		out.sort()
		return out


class Solution:

	def common_element(self, v1, v2):
		v1.sort()
		v2.sort()
		ls = []
		d = dict()
		for i in v1:
			if i in d.keys():
				d[i] += 1
			else:
				d[i] = 1
		for i in v2:
			if i in d.keys() and d[i] != 0:
				ls.append(i)
				d[i] -= 1
		return ls


class Solution:

	def common_element(self, v1, v2):
		hm = {}
		res = []
		for i in v1:
			if i in hm:
				hm[i] += 1
			else:
				hm[i] = 1
		for i in v2:
			if i in hm:
				res.append(i)
				hm[i] -= 1
				if hm[i] == 0:
					del hm[i]
		res.sort()
		return res


class Solution:

	def common_element(self, v1, v2):
		hash = {}
		for i in range(len(v1)):
			if v1[i] in hash:
				hash[v1[i]] += 1
			else:
				hash[v1[i]] = 1
		ans = []
		for j in v2:
			if j in hash and hash[j] != 0:
				ans.append(j)
				hash[j] -= 1
		return sorted(ans)


class Solution:

	def common_element(self, v1, v2):
		commonhashA = {}
		commonhashB = {}
		for i in v1:
			if not i in commonhashA:
				commonhashA[i] = 1
			else:
				commonhashA[i] += 1
		for j in v2:
			if not j in commonhashB:
				commonhashB[j] = 1
			else:
				commonhashB[j] += 1
		tmp = []
		for k in v2:
			if k in commonhashA and commonhashA[k] != 0:
				commonhashA[k] -= 1
				tmp.append(k)
		tmp.sort()
		return tmp


class Solution:

	def common_element(self, v1, v2):
		a = v1
		b = v2
		list = []
		dict = {}
		for i in a:
			if i in dict:
				dict[i] += 1
			else:
				dict[i] = 1
		b.sort()
		for i in b:
			if i in dict and dict[i] > 0:
				list.append(i)
				dict[i] -= 1
		return list


class Solution:

	def common_element(self, V1, V2):
		V1.sort()
		V2.sort()
		l = []
		(i, j) = (0, 0)
		while i < len(V1) and j < len(V2):
			if V1[i] == V2[j]:
				l.append(V1[i])
				i += 1
				j += 1
			elif V1[i] > V2[j]:
				j += 1
			else:
				i += 1
		return l


class Solution:

	def common_element(self, v1, v2):
		d = dict()
		d1 = dict()
		for i in v1:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in v2:
			if i not in d1:
				d1[i] = 1
			else:
				d1[i] += 1
		ans = []
		for i in sorted(d):
			if i in d1:
				l = [i] * min(d[i], d1[i])
				ans += l
		return ans


from collections import Counter

class Solution:

	def common_element(self, v1, v2):
		c1 = Counter(v1)
		c2 = Counter(v2)
		l = []
		for (i, j) in c2.items():
			if i in c1:
				l += [i] * min(j, c1[i])
		l.sort()
		return l


class Solution:

	def common_element(self, a, b):
		v1.sort()
		v2.sort()
		s = []
		i = 0
		j = 0
		while i < len(v1) and j < len(v2):
			if v1[i] < v2[j]:
				i = i + 1
			elif v1[i] > v2[j]:
				j = j + 1
			else:
				s.append(v1[i])
				i += 1
				j += 1
		return s


from collections import defaultdict

class Solution:

	def common_element(self, v1, v2):
		ans = []
		sample = defaultdict(int)
		for c in v1:
			sample[c] += 1
		v2.sort()
		for c in v2:
			if c in sample and sample[c] > 0:
				ans.append(c)
				sample[c] -= 1
		return ans


class Solution:

	def common_element(self, v1, v2):
		ans = []
		dic = {}
		for i in range(len(v1)):
			if v1[i] in dic:
				dic[v1[i]] += 1
			else:
				dic[v1[i]] = 1
		for j in range(len(v2)):
			if v2[j] in dic.keys():
				ans.append(v2[j])
				if dic[v2[j]] == 1:
					dic.pop(v2[j])
				else:
					dic[v2[j]] -= 1
		ans.sort()
		return ans


class Solution:

	def common_element(self, v1, v2):
		tab = {}
		res = []
		for val in v1:
			if val in tab:
				tab[val] += 1
			else:
				tab[val] = 1
		v2.sort()
		for val in v2:
			if val in tab and tab[val] > 0:
				res.append(val)
				tab[val] -= 1
		return res


class Solution:

	def common_element(self, v1, v2):
		map1 = {}
		map2 = {}
		answer = []
		for i in v1:
			map1[i] = map1.get(i, 0) + 1
		for i in v2:
			map2[i] = map2.get(i, 0) + 1
		for key in map1.keys():
			if key in map2.keys():
				for i in range(min(map1[key], map2[key])):
					answer.append(key)
		answer.sort()
		return answer


class Solution:

	def common_element(self, v1, v2):
		d1 = {}
		d2 = {}
		for i in v1:
			d1[i] = d1.get(i, 0) + 1
		for i in v2:
			d2[i] = d2.get(i, 0) + 1
		l = set(v1).intersection(set(v2))
		l = sorted(l)
		ans = []
		for i in l:
			a = min(d1[i], d2[i])
			for j in range(a):
				ans.append(i)
		return ans


class Solution:

	def common_element(self, v1, v2):
		dic = {}
		for i in v1:
			if i in dic.keys():
				dic[i] += 1
			else:
				dic[i] = 1
		mylist = []
		for i in v2:
			if i in dic.keys() and dic[i] != 0:
				mylist.append(i)
				dic[i] -= 1
		mylist.sort()
		return mylist


class Solution:

	def common_element(self, v1, v2):
		d = {}
		r = []
		for i in v1:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in v2:
			if i in d:
				r.append(i)
				if d[i] == 1:
					del d[i]
				else:
					d[i] -= 1
		r.sort()
		return r


class Solution:

	def common_element(self, v1, v2):
		res = []
		v1.sort()
		v2.sort()
		(p1, p2) = (0, 0)
		while p1 < len(v1) and p2 < len(v2):
			if v1[p1] == v2[p2]:
				res.append(v1[p1])
				p1 += 1
				p2 += 1
			elif v1[p1] > v2[p2]:
				p2 += 1
			else:
				p1 += 1
		return res


class Solution:

	def common_element(self, v1, v2):
		res = []
		hash = {}
		for x in v1:
			if x in hash:
				hash[x] += 1
			else:
				hash[x] = 1
		for x in v2:
			if x in hash:
				hash[x] -= 1
				res.append(x)
				if hash[x] == 0:
					hash.pop(x)
		res.sort()
		return res


from collections import Counter

class Solution:

	def common_element(self, v1, v2):
		d1 = Counter(v1)
		d2 = Counter(v2)
		lst = []
		for key in d1.keys():
			if d2.get(key) != None:
				lst.extend([key] * min(d1[key], d2[key]))
		return sorted(lst)


class Solution:

	def common_element(self, v1, v2):
		res = []

		def form_maps(a):
			d = {}
			for elm in a:
				if elm not in d:
					d[elm] = 1
				else:
					d[elm] += 1
			return d
		(map1, map2) = (form_maps(v1), form_maps(v2))
		for (k, v) in map1.items():
			if k in map2:
				cnt = 0
				while cnt < min(map1[k], map2[k]):
					res.append(k)
					cnt += 1
		res.sort()
		return res


class Solution:

	def common_element(self, v1, v2):
		d = {}
		for x in v1:
			if d.get(x) == None:
				d[x] = 1
			else:
				d[x] += 1
		l = []
		for x in v2:
			if d.get(x) != None:
				if d[x] > 0:
					l.append(x)
					d[x] -= 1
		l.sort()
		return l


from collections import Counter

class Solution:

	def common_element(self, v1, v2):
		c1 = Counter(v1)
		c2 = Counter(v2)
		ans = []
		for j in c1:
			if j in c2:
				ans += [j] * min(c1[j], c2[j])
		return sorted(ans)


class Solution:

	def common_element(self, arr1, arr2):
		d1 = {}
		d2 = {}
		s = set(arr1)
		ans = []
		for i in range(len(arr1)):
			d1[arr1[i]] = d1.get(arr1[i], 0) + 1
		for i in range(len(arr2)):
			if arr2[i] in d1 and d1[arr2[i]] > 0:
				d1[arr2[i]] -= 1
				ans.append(arr2[i])
		ans.sort()
		return ans