Find the first non-repeating element in a given array arr of N integers.
Note: Array consists of only positive and negative integers and not zero.
Example 1:
Input : arr[] = {-1, 2, -1, 3, 2}
Output : 3
Explanation:
-1 and 2 are repeating whereas 3 is 
the only number occuring once.
Hence, the output is 3. 
 
Example 2:
Input : arr[] = {1, 1, 1}
Output : 0
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function firstNonRepeating() that takes an array (arr), sizeOfArray (n), and returns the first non-repeating element. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10^{7}
-10^{16} <= A_{^{i }}<= 10^{16}
{A_{i} !=0 }

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def firstNonRepeating(self, arr, n): 

        # Complete the function



Solutions:
class Solution:

	def firstNonRepeating(self, arr, n):
		from collections import Counter
		a = Counter(arr)
		for i in arr:
			if a[i] == 1:
				return i
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		d = dict()
		for i in arr:
			if i in d.keys():
				d[i] += 1
			else:
				d[i] = 1
		l = [i for i in d.keys() if d[i] == 1]
		for i in arr:
			if i in l:
				return i


from collections import OrderedDict

class Solution:

	def firstNonRepeating(self, arr, n):
		d = OrderedDict()
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		result = 0
		for i in d:
			if d[i] == 1:
				result = i
				break
		return result


from collections import OrderedDict

class Solution:

	def firstNonRepeating(self, arr, n):
		d = dict()
		ptr = 1
		for i in arr:
			if i in d:
				d[i] = 0
			else:
				d[i] = ptr
				ptr += 1
		rV = 9999999
		rK = -1
		for (k, v) in d.items():
			if v != 0 and v < rV:
				rK = k
				rV = v
		return rK


class Solution:

	def firstNonRepeating(self, arr, n):
		dict = defaultdict(int)
		for k in arr:
			dict[k] += 1
		for k in arr:
			if dict[k] == 1:
				return k
		return -1


class Solution:

	def firstNonRepeating(self, arr, n):
		dic = {}
		for i in arr:
			dic[i] = 0
		for i in arr:
			dic[i] += 1
		for i in arr:
			if dic[i] == 1:
				return i


from collections import OrderedDict

class Solution:

	def firstNonRepeating(self, arr, n):
		x = OrderedDict()
		for each in arr:
			if each in x:
				x[each] += 1
			else:
				x[each] = 1
		for (i, j) in x.items():
			if j == 1:
				return i
		return -1


class Solution:

	def firstNonRepeating(self, arr, n):
		freq = {}
		for num in arr:
			if num in freq:
				freq[num] += 1
			else:
				freq[num] = 1
		for num in arr:
			if freq[num] == 1:
				return num
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		dic = {}
		for num in arr:
			if num in dic:
				dic[num] += 1
			else:
				dic[num] = 1
		for num in arr:
			if dic[num] == 1:
				return num


class Solution:

	def firstNonRepeating(self, arr, n):
		ind = dict()
		ans = 0
		for i in range(n):
			if arr[i] in ind:
				ind[arr[i]] += 1
			else:
				ind[arr[i]] = 1
		for i in arr:
			if ind.get(i) == 1:
				ans = i
				break
		return ans


class Solution:

	def firstNonRepeating(self, arr, n):
		counts = {}
		for i in arr:
			if i in counts:
				counts[i] += 1
			else:
				counts[i] = 1
		for i in arr:
			if counts[i] == 1:
				return i


class Solution:

	def firstNonRepeating(self, arr, n):
		count = {}
		for i in range(n):
			if arr[i] in count:
				count[arr[i]] += 1
			else:
				count[arr[i]] = 1
		for j in range(n):
			if count[arr[j]] == 1:
				return arr[j]
		return -1


class Solution:

	def firstNonRepeating(self, arr, n):
		d = {}
		for i in range(n):
			if arr[i] not in d:
				d[arr[i]] = 1
			else:
				d[arr[i]] += 1
		for num in arr:
			if d[num] == 1:
				return num
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		d = {}
		for i in arr:
			if i in d:
				d[i] = 1 + d.get(i, 0)
			else:
				d[i] = 1
		for i in arr:
			if d[i] == 1:
				return i
		return None


class Solution:

	def firstNonRepeating(self, arr, n):
		counts = {}
		for val in arr:
			if val in counts:
				counts[val] += 1
			else:
				counts[val] = 1
		for val in arr:
			if counts[val] == 1:
				return val
		return None


class Solution:

	def firstNonRepeating(self, arr, n):
		dict = {}
		for num in arr:
			if num not in dict.keys():
				dict[num] = 0
			dict[num] += 1
		for i in range(n):
			if dict[arr[i]] == 1:
				return arr[i]


from collections import OrderedDict

class Solution:

	def firstNonRepeating(self, arr, n):
		g = OrderedDict()
		for i in range(n):
			if arr[i] not in g:
				g[arr[i]] = 1
			else:
				g[arr[i]] += 1
		for x in g:
			if g[x] == 1:
				return x


class Solution:

	def firstNonRepeating(self, arr, n):
		hashmap = {}
		for val in arr:
			hashmap[val] = hashmap.get(val, 0) + 1
		for val in arr:
			if hashmap[val] == 1:
				return val
		return 0


from collections import OrderedDict

class Solution:

	def firstNonRepeating(self, arr, n):
		dict1 = OrderedDict()
		for each in arr:
			if each in dict1:
				dict1[each] += 1
			else:
				dict1[each] = 1
		for (key, val) in dict1.items():
			if val == 1:
				return key
				break
		if len(dict1) == 1:
			return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		freq = {}
		for i in arr:
			try:
				freq[i] += 1
			except:
				freq[i] = 1
		for i in range(n):
			if freq[arr[i]] == 1:
				return arr[i]
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		bot = {}
		for hy in arr:
			if hy in bot:
				bot[hy] += 1
			else:
				bot[hy] = 1
		for hy in arr:
			if bot[hy] == 1:
				return hy


class Solution:

	def firstNonRepeating(self, arr, n):
		freq = {}
		for elem in arr:
			freq[elem] = freq.get(elem, 0) + 1
		for elem in arr:
			if freq[elem] == 1:
				return elem
		return -1


class Solution:

	def firstNonRepeating(self, arr, n):
		mapp = {}
		for i in arr:
			if i not in mapp:
				mapp[i] = 1
			else:
				mapp[i] += 1
		for i in arr:
			if mapp[i] == 1:
				return i


class Solution:

	def firstNonRepeating(self, arr, n):
		d = {}
		for i in arr:
			if i in d.keys():
				d[i] = d[i] + 1
			else:
				d[i] = 1
		for j in arr:
			if d[j] == 1:
				return j
		return -1


class Solution:

	def firstNonRepeating(self, arr, n):
		hash = {}
		for i in arr:
			hash[i] = hash.get(i, 0) + 1
		for i in arr:
			if hash[i] == 1:
				return i


class Solution:

	def firstNonRepeating(self, arr, n):
		dict = {}
		for i in range(n):
			try:
				dict[arr[i]] += 1
			except KeyError:
				dict[arr[i]] = 1
		for i in range(n):
			if dict[arr[i]] == 1:
				return arr[i]
		return -1


class Solution:

	def firstNonRepeating(self, arr, n):
		d = {}
		for i in range(len(arr)):
			if arr[i] in d:
				d[arr[i]] += 1
			else:
				d[arr[i]] = 1
		for i in arr:
			if d[i] == 1:
				return i


from collections import Counter

class Solution:

	def firstNonRepeating(self, arr, n):
		h = Counter(arr)
		for i in range(len(arr)):
			if h[arr[i]] == 1:
				return arr[i]
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			elif i in d:
				d[i] += 1
		for i in arr:
			if d[i] < 2:
				return i


class Solution:

	def firstNonRepeating(self, arr, n):
		dict = {}
		for i in arr:
			if i in dict.keys():
				dict[i] += 1
			else:
				dict[i] = 1
		for i in arr:
			if dict[i] == 1:
				return i
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		temp = {}
		for i in range(n):
			if arr[i] in temp:
				temp[arr[i]] += 1
			else:
				temp[arr[i]] = 1
		for i in range(n):
			if temp[arr[i]] == 1:
				return arr[i]
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		count = {}
		for i in range(len(arr)):
			el = arr[i]
			if el in count:
				count[el] += 1
			else:
				count[el] = 1
		for el in arr:
			if count[el] == 1:
				return el


class Solution:

	def firstNonRepeating(self, arr, n):
		if n == 1:
			return arr[0]
		hashMap = {}
		for i in range(n):
			if hashMap.get(arr[i], 'DNE') != 'DNE':
				hashMap[arr[i]] += 1
			else:
				hashMap[arr[i]] = 1
		for i in arr:
			if hashMap[i] == 1:
				return i


class Solution:

	def firstNonRepeating(self, arr, n):
		freq = {}
		for i in range(n):
			if arr[i] in freq:
				freq[arr[i]] += 1
			else:
				freq[arr[i]] = 1
		for i in range(n):
			if freq[arr[i]] == 1:
				return arr[i]


class Solution:

	def firstNonRepeating(self, arr, n):
		f = {}
		for i in arr:
			if i in f:
				f[i] += 1
			else:
				f[i] = 1
		for i in arr:
			if f[i] == 1:
				return i
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		count = {}
		for i in arr:
			if i not in count:
				count[i] = 1
			else:
				count[i] += 1
		indices = {}
		for (index, value) in enumerate(arr):
			if count[value] == 1:
				indices[value] = index
		min_index = min(indices.values()) if len(indices) > 0 else -1
		return arr[min_index] if min_index >= 0 else -1


import sys

class Solution:

	def firstNonRepeating(self, arr, n):
		d = {}
		for i in range(len(arr)):
			if arr[i] not in d:
				d[arr[i]] = [1, i]
			else:
				d[arr[i]] = (d[arr[i]][0] + 1, d[arr[i]][1])
		min_index = sys.maxsize
		for (k, v) in d.items():
			if v[0] == 1:
				min_index = min(min_index, v[1])
		return arr[min_index]


class Solution:

	def firstNonRepeating(self, arr, n):
		dict_ = {}
		for elem in arr:
			dict_.update({elem: dict_.get(elem, 0) + 1})
		for elem in arr:
			if dict_[elem] == 1:
				return elem
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		temp = {}
		result = 0
		minindex = 100000000
		for (index, ele) in enumerate(arr):
			if ele in temp:
				tempList = temp[ele]
				tempList.append(index)
				temp[ele] = tempList
			else:
				temp[ele] = [index]
		for key in temp.keys():
			if len(temp[key]) == 1:
				if temp[key][0] < minindex:
					minindex = temp[key][0]
					result = key
		return result


class Solution:

	def firstNonRepeating(self, arr, n):
		mp = dict()
		for i in arr:
			if i not in mp:
				mp[i] = 1
			else:
				mp[i] += 1
		for i in arr:
			if mp[i] == 1:
				return i


class Solution:

	def firstNonRepeating(self, a, n):
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		for i in a:
			if d[i] == 1:
				return i
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		d = {}
		for ele in arr:
			if ele in d:
				d[ele] += 1
			else:
				d[ele] = 1
		for ele in arr:
			if d[ele] == 1:
				return ele
		else:
			return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		dic = {}
		for elm in arr:
			if elm in dic:
				dic[elm] += 1
			else:
				dic[elm] = 1
		for elm in arr:
			if dic[elm] == 1:
				return elm
		return 0


class Solution:

	def firstNonRepeating(self, arr, n):
		dicti = {}
		for ele in arr:
			dicti[ele] = dicti.get(ele, 0) + 1
		ans = -1
		for i in range(len(arr)):
			if dicti[arr[i]] == 1:
				ans = arr[i]
				break
		return ans


class Solution:

	def firstNonRepeating(self, arr, n):
		dic = {}
		for elm in arr:
			if elm not in dic:
				dic[elm] = 1
			else:
				dic[elm] += 1
		ans = []
		for (key, value) in dic.items():
			if value == 1:
				ans.append(key)
		for elm in arr:
			if elm in ans:
				return elm
		else:
			return 0