
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Last Duplicate Element in a Sorted Array Using JavaScript
In this article, we are going to explore a Javascript program that works with a sorted array containing duplicate elements. The task is to traverse the array using a loop and identify both the index of the last duplicate element and the duplicate number.
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.
Example ?
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.
Example ?
Input
arr[] = {1, 2, 3,5, 6, 7}
Output
Last index: -1 Last duplicate value: -1
If the duplicate values are not present in the array then we have to return -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 the reduce() and indexOf() methods. Let's see them in the below section ?
Reverse traversal of the array
The Reverse traversal of the array method involves comparing the current element with the previous one as we traverse the array in reverse order.
Following is a step-by-step breakdown of finding the last index in an array ?
-
Traverse the array in reverse: We start from the last element of the array and move backward.
-
Compare current and previous elements: For each element, we compare it with the previous one in the array.
-
Identify the duplicate: If the current element is the same as the previous element, it indicates a duplicate.
-
Print the index and duplicate: Once a duplicate is found, we print the index and the duplicate value, as this will be the last duplicate due to the sorted nature of the array.
- Handle no duplicates: If no duplicate is found throughout the traversal, we print -1 to indicate that there are no duplicates.
Example
Below is an example of the Last duplicate element in a Sorted Array using reverse traversal of the array method ?
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);
Output
Last index: 5 Last duplicate value: 5
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. The 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()
An alternative approach to finding the index of the last repeated element in a sorted array. This method makes use of the reduce() and indexOf methods to efficiently locate the last duplicate.
Following is a step-by-step breakdown of the approach ?
-
Start from the last index: We begin by iterating through the array starting from the last index.
-
Check using the indexOf() method: For each element, we use the indexOf() method to find the first occurrence of the current element in the array.
-
Compare indices: If the index returned by indexOf() does not match the current index, it indicates that the current element is a duplicate and we have found the last repeated element.
- Return the index: Once a mismatch is found, we return the current index as the position of the last duplicate element.
Example
Below is an example of the Last duplicate element in a Sorted Array using reduce() and indexof() ?
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') }
Output
Last index: 5 Last duplicate value: 5
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 the 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 article, 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 had to traverse the for loop and return the index of the last duplicate element present in the array and also the duplicate number.