# 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

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.
```
 `// To print last duplicate element and its ` `// index in a sorted array ` `#include ` ` `  `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\nLast "`  `            ``"duplicate item: %d\n"``, 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; ` `} `

 `// 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 `

 `# Python3 code to print last duplicate  ` `# element and its index in a sorted array ` ` `  `def` `dupLastIndex(arr, n):  ` ` `  `    ``# if array is null or size is less  ` `    ``# than equal to 0 return ` `    ``if` `(arr ``=``=` `None` `or` `n <``=` `0``):  ` `        ``return` ` `  `    ``# compare elements and return last ` `    ``# duplicate and its index ` `    ``for` `i ``in` `range``(n ``-` `1``, ``0``, ``-``1``):  ` `         `  `        ``if` `(arr[i] ``=``=` `arr[i ``-` `1``]):  ` `            ``print``(``"Last index:"``, i, ``"\nLast"``, ` `                     ``"duplicate item:"``,arr[i]) ` `            ``return` `         `  `    ``# If we reach here, then no duplicate ` `    ``# found. ` `    ``print``(``"no duplicate found"``) ` `     `  ` `  `arr ``=` `[``1``, ``5``, ``5``, ``6``, ``6``, ``7``, ``9``] ` `n ``=` `len``(arr)  ` `dupLastIndex(arr, n) ` ` `  `# This code is contributed by  ` `# Smitha Dinesh Semwal `

 `// C# code to print last duplicate element  ` `// and its index in a sorted array ` `using` `System; ` ` `  `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])  ` `            ``{ ` `                ``Console.WriteLine(``"Last index:"` `+ i); ` `                ``Console.WriteLine(``"Last duplicate item: "` `                                ``+ arr[i]); ` `                ``return``; ` `            ``} ` `        ``} ` `         `  `        ``// If we reach here, then no duplicate ` `        ``// found. ` `        ``Console.WriteLine(``"no duplicate found"``); ` `    ``} ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `[]arr = {1, 5, 5, 6, 6, 7, 9}; ` `        ``int` `n = arr.Length; ` `         `  `        ``dupLastIndex(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

 ` 0; ``\$i``--) ` `    ``{ ` `        ``if` `(``\$arr``[``\$i``] == ``\$arr``[``\$i` `- 1]) ` `        ``{ ` `            ``echo` `"Last index:"``, ``\$i` `, ``"\n"``; ` `            ``echo` `"Last duplicate item:"``, ``\$arr``[``\$i``]; ` `            ``return``; ` `    ``} ` `} ` ` `  `    ``// If we reach here, then ` `    ``// no duplicate found. ` `    ``echo` `"no duplicate found"``; ` `} ` ` `  `// Driver Code ` `\$arr` `= ``array``(1, 5, 5, 6, 6, 7, 9); ` `\$n` `= ``count``(``\$arr``); ` `dupLastIndex(``\$arr``, ``\$n``); ` ` `  `// This code is contributed by anuj_67. ` `?> `

Output:
```Last index: 4
Last duplicate item: 6
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m

Article Tags :
Practice Tags :