# Largest Non-Repeating Element

• Difficulty Level : Hard
• Last Updated : 22 May, 2021

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:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = { 3, 1, 8, 8, 4 }
Output:
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:
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++

 `// C++ program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to find the largest unique``// element of the array``void` `LarUnEl(``int` `arr[], ``int` `N)``{``    ``// Store frequency of each``    ``// distinct array element``    ``unordered_map<``int``, ``int``> mp;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Update frequency of arr[i]``        ``mp[arr[i]]++;``    ``}` `    ``// Stores largest non-repeating``    ``// element present in the array``    ``int` `LNRElem = INT_MIN;` `    ``// Stores index of the largest``    ``// unique element of the array``    ``int` `ind = -1;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// If frequency of arr[i] is equal``        ``// to 1 and arr[i] exceeds LNRElem``        ``if` `(mp[arr[i]] == 1``            ``&& arr[i] > LNRElem) {` `            ``// Update ind``            ``ind = i;` `            ``// Update LNRElem``            ``LNRElem = arr[i];``        ``}``    ``}` `    ``// If no array element is found``    ``// with frequency equal to 1``    ``if` `(ind == -1) {``        ``cout << ind;``        ``return``;``    ``}` `    ``// Print the largest``    ``// non-repeating element``    ``cout << arr[ind];``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 3, 1, 8, 8, 4 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``LarUnEl(arr, N);``}`

## Java

 `// Java program to implement``// the above approach` `import` `java.io.*;``import` `java.util.*;` `class` `GFG {` `    ``// Function to find the largest unique``    ``// element of the array``    ``static` `void` `LarUnEl(``int` `arr[], ``int` `N)``    ``{` `        ``// Store frequency of each distinct``        ``// element of the array``        ``HashMap map``            ``= ``new` `HashMap();` `        ``// Traverse the array``        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// Update frequency of arr[i]``            ``map.put(arr[i],``                    ``map.getOrDefault(arr[i], ``0``) + ``1``);``        ``}` `        ``// Stores largest non-repeating``        ``// element present in the array``        ``int` `LNRElem = Integer.MIN_VALUE;` `        ``// Stores index of the largest``        ``// non-repeating array element``        ``int` `ind = -``1``;` `        ``// Traverse the array``        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// If frequency of arr[i] is equal``            ``// to 1 and arr[i] exceeds LNRElem``            ``if` `(map.get(arr[i]) == ``1``                ``&& arr[i] > LNRElem) {` `                ``// Update ind``                ``ind = i;` `                ``// Update LNRElem``                ``LNRElem = arr[i];``            ``}``        ``}` `        ``// If no array element is found``        ``// with frequency equal to 1``        ``if` `(ind == -``1``) {``            ``System.out.println(ind);``            ``return``;``        ``}` `        ``// Print largest non-repeating element``        ``System.out.println(arr[ind]);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int``[] arr = { ``3``, ``1``, ``8``, ``8``, ``4` `};``        ``int` `N = arr.length;``        ``LarUnEl(arr, N);``    ``}``}`

## Python3

 `# Python program to implement``# the above approach``import` `sys` `# Function to find the largest unique``# element of the array``def` `LarUnEl(arr, N):``  ` `    ``# Store frequency of each distinct``    ``# element of the array``    ``map` `=` `dict``.fromkeys(arr, ``0``);` `    ``# Traverse the array``    ``for` `i ``in` `range``(N):``      ` `        ``# Update frequency of arr[i]``        ``map``[arr[i]] ``+``=` `1``;``        ` `    ``# Stores largest non-repeating``    ``# element present in the array``    ``LNRElem ``=` `-``sys.maxsize;` `    ``# Stores index of the largest``    ``# non-repeating array element``    ``ind ``=` `-``1``;` `    ``# Traverse the array``    ``for` `i ``in` `range``(N):` `        ``# If frequency of arr[i] is equal``        ``# to 1 and arr[i] exceeds LNRElem``        ``if` `(``map``.get(arr[i]) ``=``=` `1` `and` `arr[i] > LNRElem):``            ` `            ``# Update ind``            ``ind ``=` `i;` `            ``# Update LNRElem``            ``LNRElem ``=` `arr[i];` `    ``# If no array element is found``    ``# with frequency equal to 1``    ``if` `(ind ``=``=` `-``1``):``        ``print``(ind);``        ``return``;` `    ``# Prlargest non-repeating element``    ``print``(arr[ind]);` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``3``, ``1``, ``8``, ``8``, ``4``];``    ``N ``=` `len``(arr);``    ``LarUnEl(arr, N);` `    ``# This code is contributed by shikhasingrajput`

## C#

 `// C# program to implement``// the above approach ``using` `System;``using` `System.Collections.Generic;` `class` `GFG {``     ` `    ``// Function to find the largest unique``    ``// element of the array``    ``static` `void` `LarUnEl(``int``[] arr, ``int` `N)``    ``{`` ` `        ``// Store frequency of each distinct``        ``// element of the array``        ``Dictionary<``int``,``               ``int``> map = ``new` `Dictionary<``int``,``                                        ``int``>();`` ` `        ``// Traverse the array``        ``for` `(``int` `i = 0; i < N; i++) {`` ` `            ``// Update frequency of arr[i]``            ``if` `(map.ContainsKey(arr[i]) == ``true``)``                ``map[arr[i]] += 1;``            ``else``                ``map[arr[i]] = 1;``            ``}`` ` `        ``// Stores largest non-repeating``        ``// element present in the array``        ``int` `LNRElem = Int32.MinValue;`` ` `        ``// Stores index of the largest``        ``// non-repeating array element``        ``int` `ind = -1;`` ` `        ``// Traverse the array``        ``for` `(``int` `i = 0; i < N; i++) {`` ` `            ``// If frequency of arr[i] is equal``            ``// to 1 and arr[i] exceeds LNRElem``            ``if` `(map[arr[i]] == 1``                ``&& arr[i] > LNRElem) {`` ` `                ``// Update ind``                ``ind = i;`` ` `                ``// Update LNRElem``                ``LNRElem = arr[i];``            ``}``        ``}`` ` `        ``// If no array element is found``        ``// with frequency equal to 1``        ``if` `(ind == -1) {``            ``Console.WriteLine(ind);``            ``return``;``        ``}`` ` `        ``// Print largest non-repeating element``        ``Console.WriteLine(arr[ind]);``    ``}``     ` `    ``// Drivers Code``    ``public` `static` `void` `Main ()``    ``{``        ``int``[] arr = { 3, 1, 8, 8, 4 };``        ``int` `N = arr.Length;``        ``LarUnEl(arr, N);``    ``}`` ` `}` `// This code is contributed by susmitakundugoaldanga`

## Javascript

 ``
Output:
`4`

Time complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up