Java Program for Last duplicate element in a sorted array Last Updated : 09 Jun, 2022 Summarize Comments Improve Suggest changes Share 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: 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. Java // Java code to print last duplicate element // and its index in a sorted array import java.io.*; class GFG { static void dupLastIndex(int arr[], int 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 (int i = n - 1; i > 0; i--) { if (arr[i] == arr[i - 1]) { System.out.println("Last index:" + i); System.out.println("Last duplicate item: " + arr[i]); return; } } // If we reach here, then no duplicate // found. System.out.print("no duplicate found"); } // Driver code public static void main (String[] args) { int arr[] = {1, 5, 5, 6, 6, 7, 9}; int n = arr.length; dupLastIndex(arr, n); } } // This code is contributed by vt_m 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. Please refer complete article on Last duplicate element in a sorted array for more details! Comment More infoAdvertise with us Next Article How to Remove Duplicates from ArrayList in Java K kartik Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Find first and last element of ArrayList in java Prerequisite: ArrayList in Java Given an ArrayList, the task is to get the first and last element of the ArrayList in Java, Examples: Input: ArrayList = [1, 2, 3, 4] Output: First = 1, Last = 4 Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] Output: First = 12, Last = 89 Approach: Get the ArrayList 2 min read Remove repeated elements from ArrayList in Java Prerequisite: ArrayList in Java Given an ArrayList, the task is to remove repeated elements of the ArrayList in Java. Examples: Input: ArrayList = [1, 2, 2, 3, 4, 4, 4] Output: [1, 2, 3, 4] Input: ArrayList = [12, 23, 23, 34, 45, 45, 45, 45, 57, 67, 89] Output: [12, 23, 34, 45, 57, 67, 89] Below are 3 min read Get first and last elements from ArrayList in Java Given an array list, find the first and last elements of it. Examples: Input : aList = {10, 30, 20, 14, 2} Output : First = 10, Last = 2 Input : aList = {10, 30, 40, 50, 60} Output : First = 10, Last = 60 The last element is at index, size - 1 and the first element is stored at index 0. If we know h 3 min read How to Remove Duplicates from ArrayList in Java Given an ArrayList with duplicate values, the task is to remove the duplicate values from this ArrayList in Java. Examples: Input: List = [1, 10, 2, 2, 10, 3, 3, 3, 4, 5, 5] Output: List = [1, 10, 2, 3, 4, 5] Input: List = ["G", "e", "e", "k", "s"] Output: List = ["G", "e", "k", "s"] Using Iterator 4 min read How to Get Last Element in Array in Java? In Java, to get the last element in an array, we can access the element at the index array.length - 1 using array indexing. The length property of the array provides its total size, and subtracting one from it gives the index of the last element.Example 1: Here, we will access the last element in an 2 min read How to sort an ArrayList in Descending Order in Java Given an unsorted ArrayList, the task is to sort this ArrayList in descending order in Java. Examples: Input: Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Output: Sorted ArrayList: [GeeksForGeeks, Geeks, ForGeeks, For, A computer portal] Input: Unsorted ArrayList: [Ge 2 min read Like