Given a list of N words. Count the number of words that appear exactly twice in the list.
Example 1:
Input:
N = 3
list = {Geeks, For, Geeks}
Output: 1
Explanation: 'Geeks' is the only word that 
appears twice. 
Example 2:
Input:
N = 8
list = {Tom, Jerry, Thomas, Tom, Jerry, 
Courage, Tom, Courage}
Output: 2
Explanation: 'Jerry' and 'Courage' are the 
only words that appears twice. 
Your Task:  
You dont need to read input or print anything. Complete the function countWords() which takes integer N and list of strings as input parameters and returns the number of words that appear twice in the list.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)  
Constraints:
1<= N <= 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def countWords(self,List, n):
        #code here

Solutions:
class Solution:

	def countWords(self, List, n):
		x = 0
		arr = []
		for i in List:
			if i not in arr:
				arr.append(i)
				c = List.count(i)
				if c == 2:
					x += 1
		return x


class Solution:

	def countWords(self, List, n):
		from collections import defaultdict
		dic = defaultdict(int)
		for i in List:
			dic[i] += 1
		cnt = 0
		for i in dic:
			if dic[i] == 2:
				cnt += 1
		return cnt


class Solution:

	def countWords(self, List, n):
		d = {}
		for i in List:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		count = 0
		for i in d:
			if d[i] == 2:
				count += 1
		return count


from collections import Counter

class Solution:

	def countWords(self, List, n):
		ot = 0
		z = Counter(List)
		for i in z:
			if z[i] == 2:
				ot += 1
		return ot


class Solution:

	def countWords(self, List, n):
		c = 0
		for i in set(List):
			if List.count(i) == 2:
				c += 1
		return c


from collections import Counter

class Solution:

	def countWords(self, List, n):
		a = Counter(List)
		count = 0
		for i in a:
			if a[i] == 2:
				count = count + 1
		return count


class Solution:

	def countWords(self, List, n):
		dict1 = {}
		count = 0
		for i in List:
			if i in dict1:
				dict1[i] += 1
			else:
				dict1[i] = 1
		for (i, j) in dict1.items():
			if j == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		dic = {}
		for i in List:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		lis = dic.keys()
		count = 0
		for j in lis:
			if dic[j] == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		my_dict = dict()
		for item in List:
			try:
				my_dict[item] += 1
			except KeyError:
				my_dict[item] = 1
		return len(list(filter(lambda x: x == 2, my_dict.values())))


class Solution:

	def countWords(self, List, n):
		d = {}
		j = 0
		for i in List:
			if i not in d:
				d[i] = j + 1
			else:
				c = d[i]
				d[i] = c + 1
		ans = {x for x in d if d[x] == 2}
		return len(ans)


class Solution:

	def countWords(self, listt, n):
		d = {}
		for i in listt:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		z = 0
		for i in d:
			if d[i] == 2:
				z += 1
		return z


class Solution:

	def countWords(self, List, n):
		arr = list(set(List))
		counter = 0
		for i in arr:
			if List.count(i) == 2:
				counter += 1
		return counter


class Solution:

	def countWords(self, lst, n):
		count = 0
		dicti = {}
		for i in lst:
			if i not in dicti:
				dicti[i] = 1
			else:
				dicti[i] += 1
		for i in dicti:
			if dicti[i] == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		s = {}
		c = 0
		for i in List:
			if i not in s:
				s[i] = 1
			else:
				s[i] += 1
		for i in s:
			if s[i] == 2:
				c += 1
		return c


from collections import *

class Solution:

	def countWords(self, List, n):
		a = Counter(List)
		res = []
		for (key, val) in a.items():
			if val == 2:
				res.append(key)
		return len(res)


class Solution:

	def countWords(self, List, n):
		dic = {}
		count = 0
		for i in range(n):
			if List[i] in dic:
				dic[List[i]] += 1
			else:
				dic[List[i]] = 1
		for (x, y) in dic.items():
			if y == 2:
				count += 1
		return count


from operator import countOf

class Solution:

	def countWords(self, List, n):
		d = {}
		for i in List:
			if i not in d:
				d[i] = 0
			d[i] += 1
		return countOf(d.values(), 2)


from collections import defaultdict

class Solution:

	def countWords(self, List, n):
		rec = defaultdict(int)
		count = 0
		for x in List:
			rec[x] += 1
		for (k, v) in rec.items():
			if v == 2:
				count += 1
		return count


class Solution:

	def countWords(self, l, n):
		freq = {}
		for i in l:
			if i not in freq:
				freq[i] = 0
			freq[i] += 1
		c = 0
		for i in freq:
			if freq[i] == 2:
				c += 1
		return c


class Solution:

	def countWords(self, List, n):
		D = {}
		for i in List:
			D[i] = D.get(i, 0) + 1
		count = 0
		for i in D.values():
			if i == 2:
				count += 1
		return count


from collections import defaultdict

class Solution:

	def countWords(self, List, n):
		count = 0
		if len(List) <= 1:
			return 0
		result = defaultdict(int)
		for element in List:
			result[element] += 1
		for (k, v) in result.items():
			if v == 2:
				count += 1
		return count


import collections

class Solution:

	def countWords(self, List, n):
		m = collections.Counter(List)
		list1 = [i for i in m if m[i] == 2]
		return len(list1)


class Solution:

	def countWords(self, List, n):
		dic = {}
		for each in List:
			if each in dic:
				dic[each] += 1
			else:
				dic[each] = 1
		res = 0
		for (i, j) in dic.items():
			if j == 2:
				res += 1
		return res


class Solution:

	def countWords(self, l, n):
		d = {}
		for i in l:
			if i not in d:
				d[i] = 0
			d[i] += 1
		count = 0
		for i in d.keys():
			if d[i] == 2:
				count += 1
		return count


class Solution:

	def countWords(self, list, n):
		c = 0
		d = {}
		for i in list:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in d:
			if d[i] == 2:
				c += 1
		return c


import collections

class Solution:

	def countWords(self, List, n):
		mp = collections.Counter(List)
		count = 0
		for (k, v) in mp.items():
			if v == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		count = 0
		dict = {}
		for i in List:
			if dict.get(i) == None:
				dict.update({i: 1})
			elif dict.get(i) == 1:
				count += 1
				dict.update({i: 2})
			elif dict.get(i) == 2:
				count -= 1
				dict.update({i: 3})
		return count


from collections import Counter

class Solution:

	def countWords(self, List, n):
		d = Counter(List)
		x = 0
		for (i, j) in d.items():
			if j == 2:
				x += 1
		return x


from collections import Counter

class Solution:

	def countWords(self, List, n):
		cnt = Counter(List)
		c = 0
		for (i, j) in cnt.items():
			if j == 2:
				c += 1
		return c


class Solution:

	def countWords(self, List, n):
		a = dict()
		for i in List:
			if i not in a:
				a[i] = 1
			else:
				a[i] += 1
		sum = 0
		for i in a:
			if a[i] == 2:
				sum += 1
		return sum


class Solution:

	def countWords(self, List, n):
		from collections import Counter
		dict = Counter(List)
		l1 = []
		res = 0
		for i in List:
			if dict[i] == 2 and i not in l1:
				l1.append(i)
				res = res + 1
		return res


class Solution:

	def countWords(self, List, n):
		mp = dict()
		count = 0
		for i in range(n):
			if List[i] in mp.keys():
				mp[List[i]] += 1
			else:
				mp[List[i]] = 1
		for (k, v) in mp.items():
			if mp[k] == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		word_count = 0
		for word in set(List):
			if List.count(word) == 2:
				word_count += 1
		return word_count


class Solution:

	def countWords(self, List, n):
		d = dict()
		c = 0
		for i in List:
			if i in d.keys():
				d[i] += 1
			else:
				d[i] = 1
		for y in d.values():
			if y == 2:
				c += 1
		return c


class Solution:

	def countWords(self, List, n):
		d = dict()
		count = 0
		for i in range(n):
			d[List[i]] = d.get(List[i], 0) + 1
		for i in d:
			if d[i] == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		dic = {}
		for i in List:
			if i not in dic:
				dic[i] = 0
			dic[i] += 1
		count = 0
		for i in list(dic.values()):
			if i == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		dic = dict()
		ans = -1
		for i in List:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		lst = list(dic.values())
		ans = lst.count(2)
		return ans


class Solution:

	def countWords(self, List, n):
		b = {}
		for i in List:
			if i in b:
				b[i] += 1
			else:
				b[i] = 1
		res = 0
		for (k, v) in b.items():
			if v == 2:
				res = res + 1
		return res


class Solution:

	def countWords(self, List, n):
		di = {}
		for i in List:
			if i not in di:
				di[i] = 1
			else:
				di[i] += 1
		li = []
		for (key, val) in di.items():
			if val == 2:
				li.append(key)
		return len(li)


class Solution:

	def countWords(self, List, n):
		c = 0
		d = {}
		for i in List:
			d[i] = d.get(i, 0) + 1
		for i in d:
			if d[i] == 2:
				c += 1
		return c


class Solution:

	def countWords(self, List, n):
		d = {}
		for i in List:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		c = 0
		for (k, v) in d.items():
			if v == 2:
				c += 1
		return c


class Solution:

	def countWords(self, List, n):
		dct = {}
		count = 0
		for w in List:
			if w in dct.keys():
				dct[w] += 1
			else:
				dct[w] = 1
		for (k, v) in dct.items():
			if v == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		count = 0
		a = []
		for i in List:
			if i not in a:
				a.append(i)
		for i in a:
			if List.count(i) == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		l = []
		k = 0
		for i in List:
			if i not in l:
				l.append(i)
				c = List.count(i)
				if c == 2:
					k += 1
		return k


class Solution:

	def countWords(self, List, n):
		mp = {}
		for i in List:
			if i not in mp:
				mp[i] = 1
			else:
				mp[i] += 1
		ans = []
		for (key, value) in mp.items():
			if value == 2:
				ans.append(key)
		return len(ans)


class Solution:

	def countWords(self, List, n):
		d = {}
		l1 = []
		for i in List:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in d:
			if d[i] == 2:
				l1.append(i)
		return len(l1)


class Solution:

	def countWords(self, lis, n):
		li = list(set(lis))
		a = 0
		for i in li:
			if lis.count(i) == 2:
				a += 1
		return a


class Solution:

	def countWords(self, List, n):
		dt = {}
		l = []
		c = 0
		for i in List:
			if i not in dt:
				dt[i] = 1
			else:
				dt[i] = dt[i] + 1
		for (i, j) in dt.items():
			if j == 2:
				l.append(i)
		return len(l)


from collections import Counter

class Solution:

	def countWords(self, List, n):
		s = 0
		c = Counter(List)
		for i in c:
			if c[i] == 2:
				s += 1
		return s


class Solution:

	def countWords(self, List, n):
		twice_count = 0
		twice_list = []
		for ele in List:
			if ele not in twice_list:
				twice_list.append(ele)
				count1 = List.count(ele)
				if count1 == 2:
					twice_count = twice_count + 1
		return twice_count


class Solution:

	def countWords(self, List, n):
		t = {}
		count = 0
		for ele in List:
			if ele in t:
				t[ele] += 1
			else:
				t[ele] = 1
		for (key, value) in t.items():
			if value == 2:
				count += 1
		return count


class Solution:

	def countWords(self, List, n):
		frequency = {}
		for item in List:
			if item in frequency:
				frequency[item] += 1
			else:
				frequency[item] = 1
		s = 0
		for (k, v) in frequency.items():
			if v == 2:
				s += 1
		return s