On a table are N cards, with a positive integer printed on the front and back of each card (possibly different).
We flip any number of cards, and after we choose one card. 
If the number X on the back of the chosen card is not on the front of any card, then this number X is good.
What is the smallest number that is good?  If no number is good, output 0.
Here, fronts[i] and backs[i] represent the number on the front and back of card i. 
A flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.
Example:
Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
Output: 2
Explanation: If we flip the second card, the fronts are [1,3,4,4,7] and the backs are [1,2,4,1,3].
We choose the second card, which has number 2 on the back, and it isn't on the front of any card, so 2 is good.
 
Note:

1 <= fronts.length == backs.length <= 1000.
1 <= fronts[i] <= 2000.
1 <= backs[i] <= 2000.

Source: leetcode | Difficulty: MEDIUM
Tags: Data structures

Starter code: class Solution:
    def flipgame(self, fronts: List[int], backs: List[int]) -> int:
        

Solutions:
class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		w = set((fronts[i] for i in range(len(fronts)) if fronts[i] == backs[i]))
		x = set()
		for a in fronts:
			if a not in w:
				x.add(a)
		for a in backs:
			if a not in w:
				x.add(a)
		if not x:
			return 0
		return min(x)


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		same = {x for (x, y) in zip(fronts, backs) if x == y}
		return min([i for i in fronts + backs if i not in same] or [0])


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		cands = set()
		blacklist = set()
		for (f, b) in zip(fronts, backs):
			if f == b:
				blacklist.add(f)
				cands.discard(f)
			else:
				if f not in blacklist:
					cands.add(f)
				if b not in blacklist:
					cands.add(b)
		return 0 if not cands else min(cands)


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		doubles = set()
		for (f, b) in zip(fronts, backs):
			if f == b:
				doubles.add(f)
		min_x = None
		for (f, b) in zip(fronts, backs):
			if f not in doubles:
				if min_x:
					min_x = min(min_x, f)
				else:
					min_x = f
			if b not in doubles:
				if min_x:
					min_x = min(min_x, b)
				else:
					min_x = b
		if not min_x:
			return 0
		return min_x


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		cands = [0] * 2001
		blacklist = [0] * 2001
		for (f, b) in zip(fronts, backs):
			if f == b:
				blacklist[f] = 1
				cands[b] = 0
			else:
				if not blacklist[f]:
					cands[f] = 1
				if not blacklist[b]:
					cands[b] = 1
		for idx in range(1, 2001):
			if cands[idx]:
				return idx
		return 0


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		res_map = {}
		for idx in range(len(fronts)):
			if fronts[idx] == backs[idx]:
				res_map.setdefault(fronts[idx], False)
				res_map[fronts[idx]] = False
			else:
				res_map.setdefault(fronts[idx], True)
				res_map.setdefault(backs[idx], True)
		for num in sorted(res_map.keys()):
			if res_map[num]:
				return num
		return 0


class Solution(object):

	def flipgame(self, fronts, backs):
		same = {x for (i, x) in enumerate(fronts) if x == backs[i]}
		ans = float('inf')
		for x in itertools.chain(fronts, backs):
			if x not in same:
				ans = min(ans, x)
		return ans if ans != float('inf') else 0


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		n = len(fronts)
		dupes = {fronts[i] for i in range(n) if fronts[i] == backs[i]}
		res = float('inf')
		for num in itertools.chain(fronts, backs):
			if num not in dupes:
				res = min(res, num)
		return res if res < float('inf') else 0


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		offlimits = set()
		for (f, b) in zip(fronts, backs):
			if f == b:
				offlimits.add(f)
		ans = math.inf
		for (f, b) in zip(fronts, backs):
			if f != b:
				if f not in offlimits:
					ans = min(ans, f)
				if b not in offlimits:
					ans = min(ans, b)
		return 0 if ans == math.inf else ans


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		not_allowed = set()
		for (f, b) in zip(fronts, backs):
			if f == b:
				not_allowed.add(f)
		res = math.inf
		for (f, b) in zip(fronts, backs):
			if f not in not_allowed:
				res = min(res, f)
			if b not in not_allowed:
				res = min(res, b)
		if math.isfinite(res):
			return res
		else:
			return 0


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		front = dict()
		back = dict()
		for i in range(len(fronts)):
			if fronts[i] in front:
				front[fronts[i]].add(i)
			else:
				front[fronts[i]] = {i}
			if backs[i] in back:
				back[backs[i]].add(i)
			else:
				back[backs[i]] = {i}
		ans = float('inf')
		aux = set()
		for elem in front:
			if elem not in aux:
				if elem not in back:
					ans = min(ans, elem)
				else:
					flag = 1
					for index in back[elem]:
						if fronts[index] == elem:
							flag = 0
					if flag:
						ans = min(ans, elem)
				aux.add(elem)
		for elem in back:
			if elem not in aux:
				if elem not in front:
					ans = min(ans, elem)
				else:
					flag = 1
					for index in front[elem]:
						if backs[index] == elem:
							flag = 0
					if flag:
						ans = min(ans, elem)
				aux.add(elem)
		return ans if ans != float('inf') else 0


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		minimum = 10000
		sames = []
		for (front, back) in zip(fronts, backs):
			if front == back:
				sames.append(front)
		for (front, back) in zip(fronts, backs):
			if front not in sames:
				minimum = min(front, minimum)
			if back not in sames:
				minimum = min(back, minimum)
		if minimum == 10000:
			return 0
		else:
			return minimum


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		bad = []
		for i in range(len(fronts)):
			if fronts[i] == backs[i]:
				bad.append(fronts[i])
		ans = 10000000000
		for i in range(len(fronts)):
			if fronts[i] not in bad:
				ans = min(ans, fronts[i])
			if backs[i] not in bad:
				ans = min(ans, backs[i])
		if ans > 0 and ans != 10000000000:
			return ans
		return 0


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		s = set(fronts).union(set(backs))
		for c in sorted(s):
			for i in range(len(fronts)):
				if fronts[i] == c and backs[i] == c:
					break
			else:
				return c
		return 0


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		exclusions = {x for (x, y) in zip(fronts, backs) if x == y}
		ans = float('inf')
		for (x, y) in zip(fronts, backs):
			if not (x in exclusions and y in exclusions):
				if x not in exclusions:
					ans = min(ans, x)
				if y not in exclusions:
					ans = min(ans, y)
		return ans if ans != float('inf') else 0


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		a = set()
		for i in range(len(fronts)):
			if fronts[i] == backs[i]:
				a.add(fronts[i])
		l = []
		for i in range(len(fronts)):
			if fronts[i] not in a:
				l.append(fronts[i])
			if backs[i] not in a:
				l.append(backs[i])
		l.sort()
		if len(l) == 0:
			return 0
		return l[0]


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		N = len(fronts)
		impossible = set([fronts[i] for i in range(N) if fronts[i] == backs[i]])
		res = float('inf')
		for i in range(len(fronts)):
			if fronts[i] != backs[i]:
				for num in [fronts[i], backs[i]]:
					if num not in impossible:
						res = min(res, num)
		return res if res != float('inf') else 0


from collections import defaultdict

class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		if len(fronts) == 1:
			if fronts[0] == backs[0]:
				return 0
			else:
				return fronts[0]
		(f, b) = (defaultdict(set), defaultdict(set))
		for i in range(len(fronts)):
			f[fronts[i]].add(backs[i])
			b[backs[i]].add(fronts[i])
		ans = float('inf')
		for i in range(len(fronts)):
			if fronts[i] not in f[fronts[i]]:
				ans = min(ans, fronts[i])
			if backs[i] not in b[backs[i]]:
				ans = min(ans, backs[i])
		if ans == float('inf'):
			return 0
		return ans


class Solution:

	def flipgame(self, fronts: List[int], backs: List[int]) -> int:
		seen = set()
		double = set()
		for i in range(len(fronts)):
			seen.add(fronts[i])
			seen.add(backs[i])
			if fronts[i] == backs[i]:
				double.add(fronts[i])
		for k in sorted(list(seen)):
			if not k in double:
				return k
		return 0