Open In App

Javascript Program for Last duplicate element in a sorted array

Last Updated : 23 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. 

Examples: 

Input : arr[] = {1, 5, 5, 6, 6, 7}
Output :
Last index: 4
Last duplicate item: 6

Input : arr[] = {1, 2, 3, 4, 5}
Output : No duplicate found

We simply iterate through the array in reverse order and compare the current and previous element. If a match is found then we print the index and duplicate element. As this is sorted array it will be the last duplicate. If no such element is found we will print the message for it.

1- for i = n-1 to 0
     if (arr[i] == arr[i-1])
        Print current element and its index.
        Return
2- If no such element found print a message 
   of no duplicate found.

Javascript




<script>
 
// JavaScript program to print last duplicate element
// and its index in a sorted array
 
    function dupLastIndex(arr, n)
    {
        // if array is null or size is less
        // than equal to 0 return
        if (arr == null || n <= 0)
            return;
           
        // compare elements and return last
        // duplicate and its index
        for (let i = n - 1; i > 0; i--)
        {
            if (arr[i] == arr[i - 1])
            {
            document.write("Last index:" + i + "<br/>");
            document.write("Last duplicate item: "
                              + arr[i] + "<br/>");
            return;
            }
        }
           
        // If we reach here, then no duplicate
        // found.
        document.write("no duplicate found");
    }
  
// Driver code
 
        let arr = [1, 5, 5, 6, 6, 7, 9];
        let n = arr.length;
        dupLastIndex(arr, n);
 
</script>


Output: 

Last index: 4
Last duplicate item: 6

 

Time Complexity: O(n), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Method2: By using reduce() and indexOf() method. We can solve this example by using the reduce and indexOf method, reduce method is used to accumulate the summarized result from array. We use indexOf method to check first occurrence of item if it is not matched with current index of element then we pass it to the result. 

Javascript




<script>
    var arr = [1, 5, 5, 6, 6, 7];
 
    function dupLastIndex(arr) {
        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('no duplicate found')
    }
 
</script>


Output:

Last index:  4
Last duplicate item :  6

Please refer complete article on Last duplicate element in a sorted array for more details!



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads