Java Program for Last duplicate element in a sorted array Last Updated : 09 Jun, 2022 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: 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 Java Program for Last duplicate element in a sorted array kartik Follow Improve Article Tags : Java Searching Java Programs DSA Arrays +1 More Practice Tags : ArraysJavaSearching Similar Reads Java Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example :  Input: arr[] = {1, 14, 2, 16, 10, 20} Output: The third Largest element is 14 Explanation: Largest element is 20, second largest element is 16 and third largest element is 1 5 min read Java Program to Remove Duplicate Elements From the Array Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted.Example:Java// Java Program to Remove Duplicate // Ele 6 min read Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.Example Input/Output:Input: arr = { 1, 2, 3, 4, 5}Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}Ou 4 min read How to find duplicate elements in a Stream in Java Given a stream containing some elements, the task is to find the duplicate elements in this stream in Java. Examples: Input: Stream = {5, 13, 4, 21, 13, 27, 2, 59, 59, 34} Output: [59, 13] Explanation: The only duplicate elements in the given stream are 59 and 13. Input: Stream = {5, 13, 4, 21, 27, 5 min read Removing last element from ArrayList in Java Given an ArrayList collection in Java, the task is to remove the last element from the ArrayList. Example: Input: ArrayList[] = [10, 20, 30, 1, 2] Output: [10, 20, 30, 1] After removing the last element 2, the ArrayList is: [10, 20, 30, 1] Input: ArrayList[] = [1, 1, 2, 2, 3] Output: [1, 1, 2, 2] Af 2 min read Java Program to Sort the Elements of an Array in Descending Order Here, we will sort the array in descending order to arrange elements from largest to smallest. The simple solution is to use Collections.reverseOrder() method. Another way is sorting in ascending order and reversing.1. Using Collections.reverseOrder()In this example, we will use Collections.reverseO 2 min read Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort() 2 min read Java Program for Sorting all array elements except one Given an array, a positive integer, sort the array in ascending order such that the element at index K in the unsorted array stays unmoved and all other elements are sorted. Examples: Input : arr[] = {10, 4, 11, 7, 6, 20} k = 2; Output : arr[] = {4, 6, 11, 7, 10, 20} Input : arr[] = {30, 20, 10} k = 3 min read Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value 3 min read Java Program to Remove Duplicate Entries from an Array using TreeSet Features of TreeSet is the primary concern it is widely used in remove duplicates in the data structure as follows: TreeSet implements the SortedSet interface. So, duplicate values are not allowed and will be leftovers.Objects in a TreeSet are stored in a sorted and ascending order.TreeSet does not 3 min read Like