C++ 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. C++ // To print last duplicate element and its // index in a sorted array #include <bits/stdc++.h> 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]) { printf("Last index: %d Last " "duplicate item: %d ", i, arr[i]); return; } } // If we reach here, then no duplicate // found. printf("no duplicate found"); } int main() { int arr[] = {1, 5, 5, 6, 6, 7, 9}; int n = sizeof(arr) / sizeof(int); dupLastIndex(arr, n); return 0; } 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 Remove duplicates from an unsorted array using STL in C++ K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Remove duplicates from a sorted array using STL in C++ Given a sorted array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: The duplicates of the array can be removed using the un 2 min read Remove duplicates from an unsorted array using STL in C++ Given an unsorted array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {1, 2, 5, 1, 7, 2, 4, 2} Output: arr[] = {1, 2, 4, 5, 7} Input: arr[] = {1, 2, 4, 3, 5, 4, 4, 2, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: The duplicates of the array can 2 min read How to Erase Duplicates and Sort a Vector in C++? In this article, we will learn how to remove duplicates and sort a vector in C++.The simplest method to remove the duplicates and sort the vector is by using sort() and unique() functions. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i 3 min read Get first and last elements from Array and Vector in CPP Given an array, find first and last elements of it. Input: {4, 5, 7, 13, 25, 65, 98} Output: First element: 4 Last element: 98 In C++, we can use sizeof operator to find number of elements in an array. CPP // C++ Program to print first and last element in an array #include <iostream> using nam 2 min read Queries for number of distinct elements from a given index till last index in an array Given a array âa[]â of size n and number of queries q. Each query can be represented by a integer m. Your task is to print the number of distinct integers from index m to n i.e till last element of the array. Examples: Input: arr[] = {1, 2, 3, 1, 2, 3, 4, 5}, q[] = {1, 4, 6, 8} Output: 5 5 3 1 In qu 6 min read Create a Sorted Array Using Binary Search Given an array, the task is to create a new sorted array in ascending order from the elements of the given array.Examples: Input : arr[] = {2, 5, 4, 9, 8} Output : 2 4 5 8 9 Input : arr[] = {10, 45, 98, 35, 45} Output : 10 35 45 45 98 The above problem can be solved efficiently using Binary Search. 9 min read Like