Last duplicate element in a sorted array
Last Updated :
03 Aug, 2022
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.
- for i = n-1 to 0
if (arr[i] == arr[i-1])
Print current element and its index.
Return - If no such element found print a message of no duplicate found.
Implementation:
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\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;
}
C
// C code To print last duplicate element and its
// index in a sorted array
#include <stdio.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\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;
}
// This code is contributed by muditj148.
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
Python3
# 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#
// 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.
PHP
<?php
// PHP 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 or $n <= 0)
return;
// compare elements and return last
// duplicate and its index
for ( $i = $n - 1; $i > 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.
?>
JavaScript
<script>
// 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])
{
document.write("Last index:" + i + "<br/>");
document.write("Last duplicate item: "
+ arr[i] + "<br/>");
return;
}
}
// If we reach here, then no duplicate
// found.
document.write("no duplicate found");
}
// Driver code
let arr = [1, 5, 5, 6, 6, 7, 9];
let n = arr.length;
dupLastIndex(arr, n);
</script>
Output: Last index: 4
Last duplicate item: 6
Time Complexity: O(n)
Auxiliary Space: O(1), as no extra space is used.
Similar Reads
Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec
14 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
Kâth Smallest Element in Unsorted Array Given an array arr[] of N distinct elements and a number K, where K is smaller than the size of the array. Find the K'th smallest element in the given array. Examples:Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3 Output: 7Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 4 Output: 10 Table of Content[Naive Ap
15 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
Check if an Array is Sorted Given an array of size n, the task is to check if it is sorted in ascending order or not. Equal values are allowed in an array and two consecutive equal values are considered sorted.Examples: Input: arr[] = [20, 21, 45, 89, 89, 90]Output: YesInput: arr[] = [20, 20, 45, 89, 89, 90]Output: YesInput: a
9 min read
Count of smaller or equal elements in sorted array Given a sorted array of size n. Find a number of elements that are less than or equal to a given element. Examples: Input : arr[] = {1, 2, 4, 5, 8, 10} key = 9 Output : 5 Elements less than or equal to 9 are 1, 2, 4, 5, 8 therefore result will be 5. Input : arr[] = {1, 2, 2, 2, 5, 7, 9} key = 2 Outp
15+ min read