Largest Non-Repeating Element
Given an array arr[] of size N, the task is to find the largest non-repeating element present in the given array. If no such element exists, then print -1.
Examples:
Input: arr[] = { 3, 1, 8, 8, 4 }
Output: 4
Explanation:
Non-repeating elements of the given array are { 1, 3, 4 }
Therefore, the largest non-repeating element of the given array is 4.
Input: arr[] = { 3, 1, 8, 8, 3 }
Output: 1
Explanation:
Non-repeating elements of the given array are { 1 }
Therefore, the largest non-repeating element of the given array is 1.
Approach: The problem can be solved using Hashing. Follow the steps below to solve the problem:
Below is implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void LarUnEl( int arr[], int N)
{
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[arr[i]]++;
}
int LNRElem = INT_MIN;
int ind = -1;
for ( int i = 0; i < N; i++) {
if (mp[arr[i]] == 1
&& arr[i] > LNRElem) {
ind = i;
LNRElem = arr[i];
}
}
if (ind == -1) {
cout << ind;
return ;
}
cout << arr[ind];
}
int main()
{
int arr[] = { 3, 1, 8, 8, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
LarUnEl(arr, N);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void LarUnEl( int arr[], int N)
{
HashMap<Integer, Integer> map
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i < N; i++) {
map.put(arr[i],
map.getOrDefault(arr[i], 0 ) + 1 );
}
int LNRElem = Integer.MIN_VALUE;
int ind = - 1 ;
for ( int i = 0 ; i < N; i++) {
if (map.get(arr[i]) == 1
&& arr[i] > LNRElem) {
ind = i;
LNRElem = arr[i];
}
}
if (ind == - 1 ) {
System.out.println(ind);
return ;
}
System.out.println(arr[ind]);
}
public static void main(String[] args)
{
int [] arr = { 3 , 1 , 8 , 8 , 4 };
int N = arr.length;
LarUnEl(arr, N);
}
}
|
Python3
import sys
def LarUnEl(arr, N):
map = dict .fromkeys(arr, 0 );
for i in range (N):
map [arr[i]] + = 1 ;
LNRElem = - sys.maxsize;
ind = - 1 ;
for i in range (N):
if ( map .get(arr[i]) = = 1 and arr[i] > LNRElem):
ind = i;
LNRElem = arr[i];
if (ind = = - 1 ):
print (ind);
return ;
print (arr[ind]);
if __name__ = = '__main__' :
arr = [ 3 , 1 , 8 , 8 , 4 ];
N = len (arr);
LarUnEl(arr, N);
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void LarUnEl( int [] arr, int N)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++) {
if (map.ContainsKey(arr[i]) == true )
map[arr[i]] += 1;
else
map[arr[i]] = 1;
}
int LNRElem = Int32.MinValue;
int ind = -1;
for ( int i = 0; i < N; i++) {
if (map[arr[i]] == 1
&& arr[i] > LNRElem) {
ind = i;
LNRElem = arr[i];
}
}
if (ind == -1) {
Console.WriteLine(ind);
return ;
}
Console.WriteLine(arr[ind]);
}
public static void Main ()
{
int [] arr = { 3, 1, 8, 8, 4 };
int N = arr.Length;
LarUnEl(arr, N);
}
}
|
Javascript
<script>
function LarUnEl(arr, N)
{
var mp = new Map();
for ( var i = 0; i < N; i++) {
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1);
}
var LNRElem = -1000000000;
var ind = -1;
for ( var i = 0; i < N; i++) {
if (mp.get(arr[i]) == 1
&& arr[i] > LNRElem) {
ind = i;
LNRElem = arr[i];
}
}
if (ind == -1) {
cout << ind;
return ;
}
document.write( arr[ind]);
}
var arr = [3, 1, 8, 8, 4 ];
var N = arr.length;
LarUnEl(arr, N);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Another Approach: Using Sorting and O(1) extra space
- sort the given array
- traverse the array from the back and for each element check if it is equal to its previous element and also its next element
- for the next element, we use a variable named temp and for the previous element check, we simply use a[i]!=a[i-1].
- if the element is not equal that element is the answer otherwise continue the traversal
- once the traversal is over and if no element is found then return -1
Below is the implementation for the same:
C++
#include <bits/stdc++.h>
using namespace std;
int Largest( int a[], int n)
{
sort(a, a + n);
int temp = a[n - 1];
for ( int i = n - 2; i >= 0; i--) {
if (temp != a[i]) {
if (i == 0)
return a[0];
if (i - 1 >= 0) {
if (a[i] != a[i - 1])
return a[i];
}
temp = a[i];
}
}
return -1;
}
int main()
{
int arr[] = { 3, 1, 8, 8, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << Largest(arr, N);
}
|
Java
import java.util.Arrays;
public class Main {
public static int largest( int [] a, int n) {
Arrays.sort(a);
int temp = a[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i--) {
if (temp != a[i]) {
if (i == 0 ) return a[ 0 ];
if (i - 1 >= 0 ) {
if (a[i] != a[i - 1 ]) return a[i];
}
temp = a[i];
}
}
return - 1 ;
}
public static void main(String[] args) {
int [] arr = { 3 , 1 , 8 , 8 , 4 };
int n = arr.length;
System.out.println(largest(arr, n));
}
}
|
Python3
def largest(a, n):
a.sort()
temp = a[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
if temp ! = a[i]:
if i = = 0 :
return a[ 0 ]
if i - 1 > = 0 :
if a[i] ! = a[i - 1 ]:
return a[i]
temp = a[i]
return - 1
arr = [ 3 , 1 , 8 , 8 , 4 ]
N = len (arr)
print (largest(arr, N))
|
C#
using System;
class Program
{
static int Largest( int [] a, int n)
{
Array.Sort(a);
int temp = a[n - 1];
for ( int i = n - 2; i >= 0; i--)
{
if (temp != a[i])
{
if (i == 0)
return a[0];
if (i - 1 >= 0)
{
if (a[i] != a[i - 1])
return a[i];
}
temp = a[i];
}
}
return -1;
}
static void Main( string [] args)
{
int [] arr = { 3, 1, 8, 8, 4 };
int N = arr.Length;
Console.WriteLine(Largest(arr, N));
}
}
|
Javascript
function largest(a, n) {
a.sort();
let temp = a[n - 1];
for (let i = n - 2; i >= 0; i--) {
if (temp != a[i]) {
if (i == 0) return a[0];
if (i - 1 >= 0) {
if (a[i] != a[i - 1]) {
return a[i];
}
}
temp = a[i];
}
}
return -1;
}
let arr = [3, 1, 8, 8, 4];
let N = arr.length;
console.log(largest(arr, N));
|
Time Complexity: O(NlogN) Since we are using sorting
Auxiliary Space: O(1) No Extra space is used.
Last Updated :
10 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...