Given a sorted array with duplicate elements and we have to find the index of the last duplicate element and return the index of it and also return the duplicate element. 
 
Example 1:
Ã¢â¬â¹Input : arr[ ] = {1, 5, 5, 6, 6, 7}
Output : 4 6
Explanation:
Last duplicate element is 6 having index 4.Ã¢â¬â¹
Example 2:
Input : arr[ ] = {1, 2, 3, 4, 5}
Output: -1
 
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 dupLastIndex() that takes an array (arr), sizeOfArray (n), and return the two integers denoting the last index of the duplicate element and that duplicate element respectively, and if no duplicate element occurs return -1. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{7}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



def dupLastIndex (arr, n) : 

    #Complete the function

    



Solutions:
def dupLastIndex(arr, n):
	for i in range(n - 1, -1, -1):
		if i == 0:
			return (-1, '')
		if arr[i] == arr[i - 1]:
			return (i, arr[i])
			break


import collections

def dupLastIndex(arr, n):
	dict1 = collections.Counter(arr)
	for i in range(len(arr) - 1, -1, -1):
		if dict1[arr[i]] > 1:
			return [i, arr[i]]
	return [-1]


def dupLastIndex(arr, n):
	tail = n - 1
	head = tail - 1
	while head >= 0:
		if arr[head] == arr[tail]:
			return [tail, arr[tail]]
		head -= 1
		tail -= 1
	return [-1]


def dupLastIndex(arr, N):
	index = -1
	Number = -1
	for i in range(N - 1, -1, -1):
		if i == 0:
			return (-1, '')
			break
		if arr[i] == arr[i - 1]:
			return (i, arr[i])
			break


def dupLastIndex(arr, n):
	if n == 1:
		return [-1]
	if n == 2:
		if arr[0] == arr[1]:
			return [1, arr[1]]
		return [-1]
	c = 0
	for i in range(n - 1, 0, -1):
		if arr[i] == arr[i - 1]:
			c += 1
		if c > 0:
			return [i, arr[i]]
	return [-1]


from collections import Counter

def dupLastIndex(arr, n):
	li = [0, 0]
	for i in range(n - 1):
		if arr[i] == arr[i + 1]:
			li[0] = i + 1
			li[1] = arr[i]
	return li if li[0] != 0 and li[1] != 0 else (-1,)


from collections import Counter

def dupLastIndex(arr, n):
	l = []
	l1 = [-1]
	arr = arr[::-1]
	a = Counter(arr)
	for i in range(n):
		if a[arr[i]] > 1:
			l.append(n - i - 1)
			l.append(arr[i])
			break
	if len(l) > 1:
		return l
	else:
		return l1


def dupLastIndex(arr, n):
	if len(set(arr)) == n:
		return [-1]
	else:
		for i in range(n - 1, -1, -1):
			if arr.count(arr[i]) > 1:
				return (i, arr[i])


def dupLastIndex(arr, n):
	for i in range(n - 1, -1, -1):
		if arr.count(arr[i]) > 1:
			return (i, arr[i])
	return [-1]


def dupLastIndex(arr, n):
	dup = arr[0]
	dup_ind = -1
	for index in range(n - 1):
		if arr[index] == arr[index + 1]:
			dup = arr[index + 1]
			dup_ind = index + 1
	return [-1] if dup_ind == -1 else [dup_ind, dup]


def dupLastIndex(arr, n):
	ind = -1
	for i in range(1, n):
		if arr[i] == arr[i - 1]:
			ind = i
	if ind == -1:
		return (-1,)
	return (ind, arr[ind])


def dupLastIndex(arr, n):
	ind = -1
	ele = -1
	dic = {}
	for i in arr:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	for i in range(n):
		if dic[arr[i]] > 1:
			ind = i
			ele = arr[i]
	ans = []
	if ind == -1:
		ans.append(ind)
		return ans
	return (ind, ele)


def dupLastIndex(arr, n):
	res = [0, 0]
	for i in range(n - 1):
		if arr[i] == arr[i + 1]:
			res[0] = i + 1
			res[1] = arr[i]
	return res if res[0] != 0 and res[1] != 0 else (-1,)


def dupLastIndex(arr, n):
	s = {}
	for i in arr:
		if i in s:
			s[i] += 1
		else:
			s[i] = 1
	for i in range(n - 1, -1, -1):
		if s[arr[i]] > 1:
			return [i, arr[i]]
			break
	return [-1]


def dupLastIndex(arr, n):
	index = -1
	val = -1
	for i in range(n - 1, -1, -1):
		if arr[i] == arr[i - 1]:
			index = i
			val = arr[i]
			break
	if index == -1:
		return [index]
	else:
		return (index, val)


def dupLastIndex(arr, n):
	li = [0, 0]
	for i in range(n - 1, -1, -1):
		if arr[i] == arr[i - 1]:
			li[0] = i
			li[1] = arr[i]
			break
	return li if li[0] != 0 and li[1] != 0 else (-1,)


def dupLastIndex(arr, n):
	ti = 0
	t = 0
	for i in reversed(range(n)):
		if arr.count(arr[i]) > 1:
			t = arr[i]
			ti = i
			break
	if t != 0 and ti != 0:
		return (ti, t)
	li = []
	li.append(-1)
	return li


def dupLastIndex(arr, n):
	val = -1
	indx = 0
	for i in range(1, n):
		if arr[i - 1] == arr[i]:
			val = arr[i]
			indx = i
	if val == -1:
		return [val]
	else:
		return [indx, val]


def dupLastIndex(arr, n):
	d = {}
	di = {}
	a1 = []
	dic = {}
	for i in arr:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	for (k, v) in d.items():
		if v > 1:
			dic[k] = 0
	dick = list(dic.keys())
	if len(dick) == 0:
		return [-1]
	else:
		for i in arr:
			if i not in di:
				di[i] = 1
				a1.append(i)
		for i in a1:
			if i in dic.keys():
				if len(dick) == 1:
					break
				dick.remove(i)
		m = max(dick)
		for i in range(len(arr) - 1, -1, -1):
			if arr[i] == m:
				return [i, arr[i]]


def dupLastIndex(arr, n):
	index = -1
	for i in range(0, n - 1):
		if arr[i] == arr[i + 1]:
			index = i + 1
	return [index, arr[index]] if index > -1 else [index]


def dupLastIndex(arr, n):
	n = n - 1
	while n >= 1:
		if arr[n - 1] == arr[n]:
			return [n, arr[n]]
			break
		n = n - 1
	else:
		return [-1]


def dupLastIndex(arr, n):
	i = n - 1
	while i > 0:
		if arr[i] == arr[i - 1]:
			return (i, arr[i])
		i -= 1
	return [-1]


def dupLastIndex(arr, n):
	q = []
	if len(arr) != len(set(arr)):
		b = arr[::-1]
		for i in range(len(b) - 1):
			if b[i] == b[i + 1]:
				c = b[i]
				d = i
				break
		q.append(str(len(arr) - d - 1))
		q.append(str(c))
		return q
	elif len(arr) == len(set(arr)):
		q.append(-1)
		return q


def dupLastIndex(arr, n):
	while len(arr) > 1:
		item = arr.pop()
		if item == arr[-1]:
			return (len(arr), item)
	return [-1]


def dupLastIndex(arr, n):
	c = 1
	for j in range(n - 1, -1, -1):
		if arr[j] == arr[j - 1]:
			c += 1
			if c == 2:
				return [j, arr[j]]
		else:
			c = 1
	return [-1]


def dupLastIndex(arr, n):
	c = 0
	ci = 0
	for i in range(n):
		if c == arr[i]:
			ind = i
			ci = 1
		else:
			c = arr[i]
	if ci == 1:
		return (ind, arr[ind])
	else:
		return [-1]


def dupLastIndex(arr, n):
	for i in range(n - 1, -1, -1):
		lst = []
		if arr[i] == arr[i - 1]:
			lst.append(i)
			lst.append(arr[i])
			return lst
	return [-1]


def dupLastIndex(arr, n):
	k = 0
	x = 0
	for i in range(len(arr) - 1):
		if arr[i] == arr[i + 1]:
			k = i + 1
			x = arr[i + 1]
	if k == 0 and x == 0:
		return (-1,)
	else:
		return (k, x)


def dupLastIndex(arr, n):
	l1 = [0, 0]
	for i in range(len(arr) - 1):
		if arr[i] == arr[i + 1]:
			l1[0] = i + 1
			l1[1] = arr[i]
	if l1[0] == 0 and l1[1] == 0:
		return (-1,)
	else:
		return l1


def dupLastIndex(arr, n):
	ans = []
	for i in range(n - 1, 0, -1):
		if arr.count(arr[i]) > 1:
			ans.append(n - arr[::-1].index(arr[i]) - 1)
			ans.append(arr[i])
			break
	if len(ans) == 0:
		return [-1]
	else:
		return ans


def dupLastIndex(arr, n):
	dic = {}
	(dup, idx) = (0, 0)
	for i in range(n):
		if arr[i] not in dic:
			dic[arr[i]] = 1
		else:
			dup = arr[i]
			idx = i
	return [idx, dup] if dup != 0 else [-1]


def dupLastIndex(arr, n):
	lis = []
	for i in range(n - 1, 0, -1):
		if arr[i] == arr[i - 1]:
			lis.append(i)
			lis.append(arr[i])
			break
	if len(lis) == 0:
		lis.append(-1)
	return lis


def dupLastIndex(arr, n):
	dic = dict()
	lis = [0, 0]
	for i in range(n):
		if arr[i] not in dic:
			dic[arr[i]] = 1
		else:
			lis[0] = i
			lis[1] = arr[i]
	if lis[0] != 0 and lis[1] != 0:
		return lis
	else:
		return [-1]


def dupLastIndex(arr, n):
	arr.sort(reverse=True)
	for i in range(n - 1):
		if arr[i] == arr[i + 1]:
			return [n - i - 1, arr[i + 1]]
	return [-1]


def dupLastIndex(arr, n):
	d = {}
	for i in arr:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	s = list(set(arr))
	s.sort()
	val = 0
	for i in s:
		if d[i] > 1:
			val = i
	if val == 0:
		return [-1]
	else:
		ind = arr.index(val)
		index = ind + d[val] - 1
		return [index, val]


def dupLastIndex(arr, n):
	dict1 = {}
	for i in range(n):
		dict1[arr[i]] = dict1.get(arr[i], 0) + 1
	for j in range(n - 1, -1, -1):
		if dict1[arr[j]] > 1:
			return (j, arr[j])
	return [-1]


def dupLastIndex(arr, n):
	x = arr[0]
	y = -1
	for i in range(n - 1):
		if arr[i] == arr[i + 1]:
			x = arr[i]
			y = i + 1
	if y != -1:
		return [y, x]
	return [y]


def dupLastIndex(arr, n):
	ind = n - 1
	res = [-1, -1]
	while ind > 0:
		if arr[ind] == arr[ind - 1]:
			res[0] = ind
			res[1] = arr[ind]
			return res
		ind -= 1
	if res[0] == -1:
		return [-1]


def dupLastIndex(arr, n):
	ar = []
	for i in range(n - 1, -1, -1):
		if arr[i] == arr[i - 1]:
			ar.append(i)
			ar.append(arr[i])
			break
	if len(ar) != 0:
		return ar
	else:
		return [-1]


def dupLastIndex(arr, n):
	a = []
	for i in range(n - 1, -1, -1):
		if arr[i] == arr[i - 1]:
			a.append(i)
			a.append(arr[i])
			break
	if not a:
		a.append(-1)
	return a


def dupLastIndex(arr, n):
	ans = []
	for i in range(n - 1, 0, -1):
		if arr[i] == arr[i - 1]:
			ans.append(i)
			ans.append(arr[i])
			return ans
	ans.append(-1)
	return ans


def dupLastIndex(arr, n):
	k = arr[::-1]
	m = []
	s = []
	for i in k:
		if i not in m:
			m.append(i)
		else:
			s.append(n - k.index(i) - 1)
			s.append(i)
			return s
	return [-1]


def dupLastIndex(arr, n):
	for x in range(1, n):
		if arr[-x] == arr[-x - 1]:
			return (n - x, arr[-x])
	return [-1]


def dupLastIndex(arr, n):
	for i in range(n - 1, -1, -1):
		if i == 0:
			return (-1, '')
			break
		if arr[i] == arr[i - 1]:
			return (i, arr[i])
			break
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ans = dupLastIndex(arr, n)
	print(*ans)


def dupLastIndex(arr, n):
	k = []
	arr = arr[::-1]
	for i in range(n):
		if arr[i] not in k:
			k.append(arr[i])
		else:
			return [n - i, arr[i]]
	return [-1]
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ans = dupLastIndex(arr, n)
	print(*ans)


def dupLastIndex(arr, n):
	if len(arr) == len(set(arr)):
		return [-1]
	d = {}
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	ans = -1
	for i in arr:
		if d[i] > 1:
			ans = i
	index = -1
	for i in range(len(arr)):
		if arr[i] == ans:
			index = i
	return [index, ans]
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ans = dupLastIndex(arr, n)
	print(*ans)


def dupLastIndex(arr, n):
	itr = n - 1
	while itr >= 1:
		if arr[itr] == arr[itr - 1]:
			return [itr, arr[itr]]
		itr -= 1
	return [-1]
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ans = dupLastIndex(arr, n)
	print(*ans)


from collections import defaultdict

def dupLastIndex(arr, n):
	store = defaultdict(int)
	(index, value) = (0, 0)
	for i in range(len(arr) - 1):
		if arr[i] == arr[i + 1]:
			(index, value) = (i + 1, arr[i])
	if index == 0:
		return [-1]
	return [index, value]


from collections import defaultdict

def dupLastIndex(arr, n):
	store = defaultdict(int)
	for x in arr:
		store[x] = store[x] + 1
	for i in range(len(arr) - 1, -1, -1):
		if store[arr[i]] > 1:
			return [i, arr[i]]
	return [-1]