Javascript Program for Last duplicate element in a sorted array Last Updated : 13 Sep, 2024 Comments Improve Suggest changes Like Article Like 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: 4Last duplicate item: 6Input : arr[] = {1, 2, 3, 4, 5}Output : No duplicate foundMethod: 1We 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. Return2- If no such element found print a message of no duplicate found. JavaScript // 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]) { console.log("Last index:" + i); console.log("Last duplicate item: " + arr[i]); return; } } // If we reach here, then no duplicate // found. console.log("no duplicate found"); } // Driver code let arr = [1, 5, 5, 6, 6, 7, 9]; let n = arr.length; dupLastIndex(arr, n); OutputLast index:4 Last duplicate item: 6 Complexity Analysis: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 let arr = [1, 5, 5, 6, 6, 7]; function dupLastIndex(arr) { let 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') } OutputLast index: 4 Last duplicate item : 6 Please refer complete article on Last duplicate element in a sorted array for more details! Comment More infoAdvertise with us Next Article Javascript Program for Last duplicate element in a sorted array kartik Follow Improve Article Tags : Searching JavaScript Web Technologies DSA Arrays +1 More Practice Tags : ArraysSearching Similar Reads Last duplicate element in a sorted array 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 Inpu 6 min read JavaScript Program to Print All Distinct Elements in an Integer Array Given an Array of Integers consisting of n elements with repeated elements, the task is to print all the distinct elements of the array using JavaScript. We can get the distinct elements by creating a set from the integer array. Examples: Input : arr = [ 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9 ] Outpu 3 min read Javascript Program For Removing Duplicates From An Unsorted Linked List Given an unsorted Linked List, the task is to remove duplicates from the list. Examples: Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21 Output: 12 -> 11 -> 21 -> 41 -> 43 Explanation: Second occurrence of 12 and 21 are removed. Input: linked_list = 12 -> 5 min read JavaScript Program to find the Nth smallest/largest element from an unsorted Array In this article, we will see how to find Nth largest and smallest element from an unsorted array. The Nth smallest/largest element from an unsorted array refers to the element that ranks at the Nth position when the array is sorted either in ascending (smallest) or descending (largest) order. It rep 4 min read Find duplicate elements in an array Given an array of n integers. The task is to find all elements that have more than one occurrences. The output should only be one occurrence of a number irrespective of the number of occurrences in the input array.Examples: Input: {2, 10, 10, 100, 2, 10, 11, 2, 11, 2}Output: {2, 10, 11}Input: {5, 40 11 min read Find Equal (or Middle) Point in a sorted array with duplicates Given a sorted array of n size, the task is to find whether an element exists in the array from where the number of smaller elements is equal to the number of greater elements.If Equal Point appears multiple times in input array, return the index of its first occurrence. If doesn't exist, return -1. 9 min read Print sorted distinct elements of array Given an array that might contain duplicates, print all distinct elements in sorted order. Examples: Input : 1, 3, 2, 2, 1Output : 1 2 3Input : 1, 1, 1, 2, 2, 3Output : 1 2 3The simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements. Algorith 6 min read JavaScript Program to Find Element that Appears once in Array where Other Elements Appears Twice Given an Array of Integers consisting of n elements where each element appears twice except one element. The task is to find the element which appears only once in the input array in JavaScript. Example: Input : arr = [ 5, 9, 2, 7, 4, 8, 6, 1, 5, 1, 4, 6, 3, 7, 8, 2, 9 ] Output : 3Explanation: The i 3 min read Making elements distinct in a sorted array by minimum increments Given a sorted integer array. We need to make array elements distinct by increasing values and keeping array sum minimum possible. We need to print the minimum possible sum as output. Examples: Input : arr[] = { 2, 2, 3, 5, 6 } ; Output : 20 Explanation : We make the array as {2, 3, 4, 5, 6}. Sum be 10 min read Last seen array element (last appearance is earliest) Given an array that might contain duplicates, find the element whose last appearance is latest. Examples: Input : arr[] = {10, 30, 20, 10, 20} Output : 30 Explanation: Below are indexes of last appearances of all elements (0 based indexes) 10 last occurs at index 3 30 last occurs at index 1 20 last 5 min read Like