Last duplicate element in a sorted array
Basic Accuracy: 60.06% Submissions: 535 Points: 1

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
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).


1 ≤ N ≤ 105
1 ≤ A[i] ≤ 107

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Last duplicate element in a sorted array

Output Window