Given a sequence of strings, the task is to find out the second most repeated (or frequent) string in the given sequence.
Note: No two strings are the second most repeated, there will be always a single string.
Example 1:
Input:
N = 6
arr[] = {aaa, bbb, ccc, bbb, aaa, aaa}
Output: bbb
Explanation: "bbb" is the second most 
occurring string with frequency 2.
Example 2:
Input: 
N = 6
arr[] = {geek, for, geek, for, geek, aaa}
Output: for
Explanation: "for" is the second most
occurring string with frequency 2.
Your Task:
You don't need to read input or print anything. Your task is to complete the function secFrequent() which takes the string array arr[] and its size N as inputs and returns the second most frequent string in the array.
Expected Time Complexity: O(N*max(|S_{i}|).
Expected Auxiliary Space: O(N*max(|S_{i}|).
Constraints:
1<=N<=10^{3}

Difficulty: EASY
Tags: String algorithms

Data structures
Skill types: Data structures
Expected Time Complexity: O(N*max(|S_{i}|).
Expected Auxiliary Space: O(N*max(|S_{i}|).

Starter code:
#User function Template for python3

class Solution:
    def secFrequent(self, arr, n):
        # code here

Solutions:
class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		a = sorted(d.items(), key=lambda x: x[1])
		return a[-2][0]


class Solution:

	def secFrequent(self, arr, n):
		dic = {}
		for ele in arr:
			dic[ele] = dic.get(ele, 0) + 1
		max_freq = max(dic.values())
		a = [x for x in dic.values() if x != max_freq]
		a.sort()
		for x in dic:
			if dic[x] == a[-1]:
				return x
		return '-1'


class Solution:

	def secFrequent(self, arr, n):
		s = {}
		for i in arr:
			if i not in s:
				s[i] = 1
			else:
				s[i] = s[i] + 1
		lar = -1
		pos = -1
		for i in s:
			if lar < s[i]:
				lar = s[i]
				pos = i
		slar = -1
		spos = -1
		for i in s:
			if s[i] > slar and s[i] < lar:
				slar = s[i]
				spos = i
		if spos != -1:
			return spos
		else:
			return ''


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in range(len(arr)):
			if arr[i] in d:
				d[arr[i]] += 1
			else:
				d[arr[i]] = 1
		d = sorted(d.items(), key=lambda x: x[1])
		m = d[-2]
		return m[0]


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		a = []
		h = {}
		for i in arr:
			if i in h:
				h[i] += 1
			else:
				h[i] = 1
		for i in h:
			a.append(h[i])
		a.sort()
		ans = a[-2]
		for i in h:
			if h[i] == ans:
				return i


class Solution:

	def secFrequent(self, arr, n):
		freq = {}
		for string in arr:
			freq[string] = freq.get(string, 0) + 1
		most_common = ('', 0)
		second_most_common = ('', 0)
		for (string, frequency) in freq.items():
			if frequency > most_common[1]:
				second_most_common = most_common
				most_common = (string, frequency)
			elif frequency > second_most_common[1] and string != most_common[0]:
				second_most_common = (string, frequency)
		return second_most_common[0]


class Solution:

	def secFrequent(self, arr, n):
		a = []
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in d:
			a.append(d[i])
		a.sort()
		res = a[-2]
		for i in d:
			if d[i] == res:
				return i


class Solution:

	def secFrequent(self, arr, n):
		dict1 = {}
		for i in arr:
			if i in dict1:
				dict1[i] += 1
			else:
				dict1[i] = 1
		a = sorted(dict1.values())
		b = max(a)
		a.remove(b)
		c = max(a)
		for i in dict1:
			if dict1[i] == c:
				return i


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		l = []
		for i in d.values():
			l.append(i)
		l.sort()
		a = l[-2]
		for (k, v) in d.items():
			if v == a:
				return k


from collections import Counter
import operator

class Solution:

	def secFrequent(self, arr, n):
		dic = {}
		for i in arr:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		lst = []
		for (i, j) in dic.items():
			lst.append([i, j])
		lst.sort(key=operator.itemgetter(1, 0), reverse=True)
		return lst[1][0]


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		d = Counter(arr)
		first = float('-inf')
		second = float('-inf')
		for val in d.values():
			if val > first:
				second = first
				first = val
			elif val > second and first != val:
				second = val
		if second == float('-inf'):
			return ''
		for (k, v) in d.items():
			if v == second:
				return k


class Solution:

	def secFrequent(self, arr, n):
		a = []
		d = dict()
		for ele in arr:
			d[ele] = 0
		for ele in arr:
			d[ele] = d[ele] + 1
		for ele in d:
			a.append(d[ele])
		a.sort()
		b = a[-2]
		for i in d:
			if d[i] == b:
				return i


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for word in arr:
			d.setdefault(word, 0)
			d[word] += 1
		f_max = float('-inf')
		s_max = float('-inf')
		for val in d:
			if d[val] > f_max:
				s_max = f_max
				f_max = d[val]
			if d[val] > s_max and d[val] != f_max:
				s_max = d[val]
		for i in d:
			if d[i] == s_max:
				return i
		return ''


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for word in arr:
			d.setdefault(word, 0)
			d[word] += 1
		val = list(d.values())
		val.sort()
		mx = val[-2]
		for i in d:
			if d[i] == mx:
				return i


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		freq = Counter(arr)
		items = list(freq.items())
		items.sort(key=lambda x: x[1])
		return items[-2][0]


from collections import Counter

def compare(item):
	return item[1]

class Solution:

	def secFrequent(self, arr, n):
		freq = Counter(arr)
		items = list(freq.items())
		items.sort(key=compare)
		return items[-2][0]


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		charFreq = Counter(arr)
		items = list(charFreq.items())
		items.sort(key=lambda x: x[1], reverse=True)
		return items[1][0]


class Solution:

	def secFrequent(self, arr, n):
		freq = {}
		for i in range(n):
			freq[arr[i]] = freq.get(arr[i], 0) + 1
		max_freq = -1
		sec_max_freq = -1
		max_freq_str = ''
		sec_max_freq_str = ''
		for key in freq:
			if freq[key] > max_freq:
				sec_max_freq = max_freq
				sec_max_freq_str = max_freq_str
				max_freq = freq[key]
				max_freq_str = key
			elif freq[key] > sec_max_freq and freq[key] != max_freq:
				sec_max_freq = freq[key]
				sec_max_freq_str = key
		return sec_max_freq_str if freq else None


class Solution:

	def secFrequent(self, arr, n):
		dict1 = {}
		for i in arr:
			if i in dict1:
				dict1[i] += 1
			else:
				dict1[i] = 1
		m = sorted(dict1.values())[-2]
		for i in arr:
			if dict1[i] == m:
				return i


class Solution:

	def secFrequent(self, arr, n):
		dictt = {}
		for i in arr:
			if i in dictt:
				dictt[i] += 1
			else:
				dictt[i] = 1
		k = sorted(dictt.values())[-2]
		for i in dictt:
			if dictt[i] == k:
				return i


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		dict1 = {}
		for i in arr:
			if i in dict1:
				dict1[i] += 1
			else:
				dict1[i] = 1
		a = sorted(dict1.values())
		b = max(a)
		a.remove(b)
		c = max(a)
		for i in dict1:
			if dict1[i] == c:
				return i


import operator

class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		l = [[i, d[i]] for i in d]
		l = sorted(l, reverse=True, key=operator.itemgetter(1))
		return l[1][0]


class Solution:

	def secFrequent(self, arr, n):
		res = []
		freq = {}
		for i in arr:
			freq[i] = freq.get(i, 0) + 1
		for i in freq:
			res.append(freq[i])
		res.sort()
		ans = res[-2]
		for i in freq:
			if freq[i] == ans:
				return i


class Solution:

	def secFrequent(self, arr, n):
		a = []
		d = {}
		for i in arr:
			d[i] = d.get(i, 0) + 1
		for i in d:
			a.append(d[i])
		a.sort()
		ans = a[-2]
		for i in d:
			if d[i] == ans:
				return i


class Solution:

	def secFrequent(self, arr, n):
		lst = []
		dic = {}
		for i in arr:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
		for i in dic:
			lst.append(dic[i])
		lst.sort()
		for i in dic:
			if dic[i] == lst[-2]:
				return i


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		d1 = sorted(d.values())
		z = d1[-2]
		for i in d:
			if d[i] == z:
				return i


class Solution:

	def secFrequent(self, arr, n):
		dir1 = {}
		ans = []
		for i in arr:
			dir1[i] = dir1.get(i, 0) + 1
		ans = list(dir1.values())
		ans.sort()
		ans2 = ans[-2]
		for i in dir1:
			if dir1[i] == ans2:
				return i


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		f = 0
		for i in arr:
			d.update({i: arr.count(i)})
		max = -1
		for i in d.values():
			if i > max:
				max = i
		max1 = -1
		for i in d.values():
			if i > max1 and i < max:
				max1 = i
		for i in d.keys():
			if d[i] == max1:
				f = 1
				return i
		if f == 0:
			return ''


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			d[i] = d.get(i, 0) + 1
		l = []
		for i in d:
			l.append((d[i], i))
		l.sort()
		return l[-2][1]


class Solution:

	def secFrequent(self, arr, n):
		l = list(set(arr))
		l2 = []
		l3 = []
		for i in l:
			cnt = arr.count(i)
			l2.append(cnt)
			s = sorted(l2)
		res = s[-2]
		for i in l:
			cnt = arr.count(i)
			if cnt == res:
				l3.append(i)
		return l3[0]


class Solution:

	def secFrequent(self, arr, n):
		dic = {}
		for i in arr:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
		sorted_dic = sorted(dic.items(), key=lambda item: item[1])
		return sorted_dic[-2][0]


class Solution:

	def secFrequent(self, arr, n):
		dct = {}
		for i in range(n):
			if arr[i] in dct:
				dct[arr[i]] += 1
			else:
				dct[arr[i]] = 1
		k = 0
		s = ''
		for i in dct:
			if dct[i] > k:
				s = i
				k = dct[i]
		dct.pop(s)
		k = 0
		s = ''
		for i in dct:
			if dct[i] > k:
				s = i
				k = dct[i]
		return s


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		m = 0
		for i in d:
			if d[i] > m:
				m = d[i]
		m1 = 0
		mi = ''
		for i in d:
			if d[i] > m1 and d[i] != m:
				m1 = d[i]
				mi = i
		return mi


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		(fs, ss) = ('', '')
		(m, ms) = (0, 0)
		for i in d:
			if d[i] >= m:
				(ms, ss) = (m, fs)
				(m, fs) = (d[i], i)
			elif d[i] > ms:
				(ms, ss) = (d[i], i)
		return ss


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		res = []
		for i in d:
			res.append([i, d[i]])
		if len(res) == 1:
			return ''
		res.sort(key=lambda x: x[1])
		return res[len(res) - 2][0]


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		d = sorted(Counter(arr).items(), key=lambda x: x[1], reverse=True)
		return d[1][0]


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		l = -1
		s = -1
		for i in arr:
			if d[i] > l:
				s = l
				l = d[i]
			elif d[i] > s and d[i] < l:
				s = d[i]
		if s == -1:
			return ''
		else:
			for i in arr:
				if d[i] == s:
					return i


class Solution:

	def secFrequent(self, arr, n):
		freq = {}
		for word in arr:
			if word in freq:
				freq[word] += 1
			else:
				freq[word] = 1
		max1 = 0
		max2 = 0
		res1 = ''
		res2 = ''
		for (val, key) in freq.items():
			if key > max1:
				max2 = max1
				max1 = key
				res2 = res1
				res1 = val
			elif key > max2:
				max2 = key
				res2 = val
		return res2


class Solution:

	def secFrequent(self, arr, n):
		if len(arr) == 0:
			return False
		my_dict = {}
		for i in arr:
			if i in my_dict:
				my_dict[i] += 1
			else:
				my_dict[i] = 1
		sorted_dict = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
		return sorted_dict[1][0]


class Solution:

	def secFrequent(self, arr, n):
		dic = {}
		for i in arr:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
		x = []
		for i in dic.keys():
			x.append(dic[i])
		x.sort()
		x.reverse()
		for i in dic.keys():
			if dic[i] == x[1]:
				return i


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			d[i] = 0
		for i in arr:
			d[i] += 1
		mx = 0
		val = ''
		for i in arr:
			if d[i] > mx:
				mx = d[i]
		mx2 = 0
		for i in arr:
			if mx > d[i] > mx2:
				mx2 = d[i]
				val = i
		return val


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		all_strings = dict(Counter(arr))
		store = []
		for i in sorted(all_strings, key=all_strings.get):
			if i not in store:
				store.append(i)
		return store[-2]


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		ans = Counter(arr)
		f = True
		fm = 0
		ele1 = ''
		if len(ans) == 1:
			return ''
		for (i, j) in ans.items():
			if f:
				fm = j
				ele1 = i
				f = False
			if j > fm:
				fm = j
				ele1 = i
		ans.pop(ele1)
		v = fm
		f = True
		for (i, j) in ans.items():
			if f:
				fm = j
				ele1 = i
				f = False
			if j > fm:
				fm = j
				ele1 = i
		if v == fm:
			return ''
		return ele1


class Solution:

	def secFrequent(self, arr, n):
		hmap = {}
		for i in range(n):
			if arr[i] in hmap:
				hmap[arr[i]] += 1
			else:
				hmap[arr[i]] = 1
		sorted_hmap = sorted(hmap.items(), key=lambda x: x[1], reverse=True)
		return sorted_hmap[1][0]


class Solution:

	def secFrequent(self, arr, n):
		freq = {}
		for i in arr:
			if i not in freq:
				freq[i] = 0
			freq[i] += 1
		if len(freq) == 1:
			return ''
		lis = list(freq.items())
		lis.sort(key=lambda x: x[1], reverse=True)
		return lis[1][0]


class Solution:

	def secFrequent(self, arr, n):
		dic = {}
		maxx = 0
		for each in arr:
			if each not in dic:
				dic[each] = 1
			else:
				dic[each] += 1
			maxx = max(maxx, dic[each])
		(wrd, freq) = ('', 0)
		for (i, j) in dic.items():
			if j > freq and j < maxx:
				(wrd, freq) = (i, j)
		return wrd


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		c = Counter(arr)
		l = [i for i in c.values()]
		l.sort(reverse=True)
		x = l[1]
		for i in c:
			if c[i] == x:
				return i


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		l = []
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		s = sorted(d.values())
		for i in d:
			if d[i] == s[-2]:
				return i


from collections import Counter

class Solution:

	def secFrequent(self, a, n):
		d = {}
		for i in range(len(arr)):
			if arr[i] in d:
				d[arr[i]] += 1
			else:
				d[arr[i]] = 1
		d = sorted(d.items(), key=lambda x: x[1], reverse=True)
		m = d[1]
		return m[0]


class Solution:

	def secFrequent(self, arr, n):
		dict = {}
		for i in arr:
			if i in dict:
				dict[i] += 1
			else:
				dict[i] = 1
		lst = []
		for i in dict:
			lst.append(dict[i])
		m = max(lst)
		lst.remove(m)
		m = max(lst)
		for i in dict:
			if dict[i] == m:
				return i


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		c = Counter(arr)
		c = sorted(c.items(), reverse=True, key=lambda kv: kv[1])
		return c[1][0]


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		c = Counter(arr)
		m = sorted(c.values())[-2]
		for (k, v) in c.items():
			if v == m:
				return k


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		l = []
		for (k, v) in d.items():
			l.append(v)
		l = sorted(l)
		for (k, v) in d.items():
			if v == l[-2]:
				return k


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in range(len(arr)):
			if arr[i] not in d:
				d[arr[i]] = 1
			else:
				d[arr[i]] += 1
		if len(d) >= 2:
			first = 0
			second = 0
			for i in d:
				if d[i] > first:
					second = first
					first = d[i]
				elif d[i] > second and d[i] != first:
					second = d[i]
			for word in d:
				if d[word] == second:
					return word
		else:
			return ''


class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		val = sorted(d.values(), reverse=True)
		second = val[1]
		for (key, value) in d.items():
			if value == second:
				return key


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		count = Counter(arr)
		val = sorted(count.values(), reverse=True)
		max_val = val[1]
		for (i, j) in count.items():
			if j == max_val:
				return i


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		d = Counter(arr)
		val = sorted(d.values(), reverse=True)
		second = val[1]
		for (k, v) in d.items():
			if v == second:
				return k


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		d = dict()
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		a = []
		for (i, j) in d.items():
			a.append(j)
		if len(a) <= 1:
			return ''
		else:
			m = max(a)
			c = 0
			for i in range(len(a)):
				if a[i] < m:
					c = max(c, a[i])
			for (i, j) in d.items():
				if j == c:
					return i


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		mx = max(list(d.values()))
		d1 = {}
		for (i, j) in d.items():
			if j == mx:
				continue
			else:
				d1[i] = j
		mx1 = max(list(d1.values()))
		for (i, j) in d1.items():
			if j == mx1:
				return i


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		d = sorted(d.items(), key=lambda x: x[1])
		m = d[-2]
		return m[0]


from collections import defaultdict

class Solution:

	def secFrequent(self, arr, n):
		d = defaultdict(int)
		for i in arr:
			d[i] += 1
		max1 = -1
		max2 = -1
		for i in d:
			if max1 < d[i]:
				max2 = max1
				max1 = d[i]
			elif max2 < d[i] and d[i] != max1:
				max2 = d[i]
		for i in d:
			if d[i] == max2:
				return i
		return ''


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		con = Counter(arr)
		res = sorted(con.values(), reverse=True)
		maxi = res[1]
		for (key, val) in con.items():
			if val == maxi:
				return key


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		c = Counter(arr)
		return c.most_common(2)[1][0]


from collections import Counter

class Solution:

	def secFrequent(self, arr, n):
		d = Counter(arr)
		li = d.most_common(2)
		return li[1][0]