# JavaScript Program for the Last duplicate element in a Sorted Array

In this program, we are given a sorted array with duplicate elements and we have to traverse the for loop and return the index of the last duplicate element present in the array and also the duplicate number, which is going to see in the article below.

### Introduction to Problem

In the given problem we have to find the last duplicate element in a sorted array. Duplicate means the number is present more than one time, so here we have given a sorted array (increasing order) from which we have to find the index of the last duplicate elements and also the value of it. We have to return both index and value of the last duplicate number if the duplicate number is not present we have to return ‘-1’.

For example,

We are given a sorted array containing duplicate numbers −

Input: arr[] = {1, 2, 2, 3, 5, 5, 6, 7}
Output:
Last index: 5
Last duplicate value: 5


From the above given sorted array, we have duplicate values are 2 and 5. And 5 is the last duplicate number in the index 5. So we have returned 5 and 5 in the last index and last duplicate value respectively.

If the duplicate values are not present in the array then we have to return -1. Let’s see an example of it −

Input: arr[] = {1, 2, 3,5, 6, 7}
Output:
Last index: -1
Last duplicate value: -1


## Approach

We have seen the example above for the current problem. Now, let's discuss some of the approaches. here we are going to discuss two approaches first approach is the reverse traversal of the array and the second approach is by using reduce() and indexOf() methods. Let's see them in the below section −

## Approach 1: Reverse traversal of the array

Here we first compare the current and previous elements of the array as we traverse through the array in reverse order. If both current and previous values are the same it means we got the duplicate value then the index and duplicate element are printed. This will be the final duplicate because the array is sorted. If the duplicate value is not present, then we can print the ‘-1’.

### Example

JavaScript program to print the last duplicate element and its index in a sorted array.

function dupLastIndex(array, num) {
// if the size of array is less than or equal to 0
if (num <= 0){
document.write("-1");
}

// compare current and previous elements
// if they matched return the index and value of the last duplicate number

for (let i = num - 1; i > 0; i--){
if (array[i] == array[i - 1]){
console.log("Last index:" + i);
console.log("Last duplicate value: " + array[i] );
return;
}
}

// If duplicate number is not present return -1
console.log("-1");
}
let array = [1, 2, 2, 3, 5, 5, 6, 7];
let num = array.length;
dupLastIndex(array, num);

### Time and Space Complexity

The time complexity of the above code is O(N) where N is the size of the sorted array. Because we have only traversed the array from the end of the array. And Space complexity of the above code is O(1) because we don’t require any extra space, we only use constate space.

## Approach 2: Using reduce() and indexOf()

We have seen the above approach in which all the duplicates are present next to each other. Here we are using the concept of the reduce and indexOf methods to get the index of the last repeated element in the array. We will start from the last index and check by using the indexOf method, what is the index of the given current element. As the indexOf() method returns the first index of the current element, means that we can check the returned and the current index if they both don’t match then we can return the index of the current element as the answer.

### Example

var arr = [1, 2, 2, 3, 5, 5, 6, 7];
function dupLastIndex(array) {
var unique = arr.reduce((acc, iter, i)=>
arr.indexOf(iter)!= i ? i : acc, -1);
return unique;
}
let temp = dupLastIndex(arr);
if( temp != -1){
console.log('Last index: ',temp);
console.log('Last duplicate item : ',arr[temp])
} else {
console.log('-1')
}

### Time and Space Complexity

The time complexity of the above code is O(N*N) where N is the size of the array because we are traversing over the complete array and indexOf() method can also take O(N) time for each iteration. The space complexity of the given code is O(1) as we are not using any extra space.

## Conclusion

In this tutorial, we have implemented a JavaScript program for finding the last duplicate element in a sorted array. We were given a sorted array with duplicate elements and we have to traverse the for loop and return the index of the last duplicate element present in the array and also the duplicate number. We have used two methods that works on the O(N) and O(N*N) time complexity and both works on O(1) space complexity.

Updated on: 30-Mar-2023

162 Views 