# Find the only non-repeating element in a given array

• Last Updated : 07 Aug, 2021

Given an array A[] consisting of N (1 ≤ N ≤ 105) positive integers, the task is to find the only array element with a single occurrence.

Note: It is guaranteed that only one such element exists in the array.

Examples:

Input: A[] = {1, 1, 2, 3, 3}
Output: 2
Explanation:
Distinct array elements are {1, 2, 3}.
Frequency of these elements are {2, 1, 2} respectively.

Input : A[] = {1, 1, 1, 2, 2, 3, 5, 3, 4, 4}
Output : 5

Approach: Follow the steps below to solve the problem

1. Traverse the array
2. Use an Unordered Map to store the frequency of array elements.
3. Traverse the Map and find the element with frequency 1 and print that element.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the``// above approach` `#include ``using` `namespace` `std;` `// Function call to find``// element in A[] with frequency = 1``void` `CalcUnique(``int` `A[], ``int` `N)``{``    ``// Stores frequency of``    ``// array elements``    ``unordered_map<``int``, ``int``> freq;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Update frequency of A[i]``        ``freq[A[i]]++;``    ``}` `    ``// Traverse the Map``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// If non-repeating element``        ``// is found``        ``if` `(freq[A[i]] == 1) {` `            ``cout << A[i];``            ``return``;``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `A[] = { 1, 1, 2, 3, 3 };``    ``int` `N = ``sizeof``(A) / ``sizeof``(A);` `    ``CalcUnique(A, N);` `    ``return` `0;``}`

## Java

 `// Java implementation of the``// above approach``import` `java.util.*;``class` `GFG``{` `  ``// Function call to find``  ``// element in A[] with frequency = 1``  ``static` `void` `CalcUnique(``int` `A[], ``int` `N)``  ``{``    ``// Stores frequency of``    ``// array elements``    ``HashMap freq = ``new` `HashMap();` `    ``// Traverse the array``    ``for` `(``int` `i = ``0``; i < N; i++)``    ``{` `      ``// Update frequency of A[i]``      ``if``(freq.containsKey(A[i]))``      ``{``        ``freq.put(A[i], freq.get(A[i]) + ``1``);``      ``}``      ``else``      ``{``        ``freq.put(A[i], ``1``);``      ``}``    ``}` `    ``// Traverse the Map``    ``for` `(``int` `i = ``0``; i < N; i++)``    ``{` `      ``// If non-repeating element``      ``// is found``      ``if` `(freq.containsKey(A[i])&&freq.get(A[i]) == ``1``)``      ``{``        ``System.out.print(A[i]);``        ``return``;``      ``}``    ``}``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `A[] = { ``1``, ``1``, ``2``, ``3``, ``3` `};``    ``int` `N = A.length;``    ``CalcUnique(A, N);``  ``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python 3 implementation of the``# above approach``from` `collections ``import` `defaultdict` `# Function call to find``# element in A[] with frequency = 1``def` `CalcUnique(A, N):` `    ``# Stores frequency of``    ``# array elements``    ``freq ``=` `defaultdict(``int``)` `    ``# Traverse the array``    ``for` `i ``in` `range``(N):` `        ``# Update frequency of A[i]``        ``freq[A[i]] ``+``=` `1` `    ``# Traverse the Map``    ``for` `i ``in` `range``(N):` `        ``# If non-repeating element``        ``# is found``        ``if` `(freq[A[i]] ``=``=` `1``):``            ``print``(A[i])``            ``return` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``A ``=` `[``1``, ``1``, ``2``, ``3``, ``3``]``    ``N ``=` `len``(A)``    ``CalcUnique(A, N)` `    ``# This code is contributed by chitranayal.`

## C#

 `// C# implementation of the``// above approach``using` `System;``using` `System.Collections.Generic;` `public` `class` `GFG``{` `  ``// Function call to find``  ``// element in []A with frequency = 1``  ``static` `void` `CalcUnique(``int` `[]A, ``int` `N)``  ``{``    ` `    ``// Stores frequency of``    ``// array elements``    ``Dictionary<``int``,``int``> freq = ``new` `Dictionary<``int``,``int``>();` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++)``    ``{` `      ``// Update frequency of A[i]``      ``if``(freq.ContainsKey(A[i]))``      ``{``        ``freq[A[i]] = freq[A[i]] + 1;``      ``}``      ``else``      ``{``        ``freq.Add(A[i], 1);``      ``}``    ``}` `    ``// Traverse the Map``    ``for` `(``int` `i = 0; i < N; i++)``    ``{` `      ``// If non-repeating element``      ``// is found``      ``if` `(freq.ContainsKey(A[i]) && freq[A[i]] == 1)``      ``{``        ``Console.Write(A[i]);``        ``return``;``      ``}``    ``}``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main(String[] args)``  ``{``    ``int` `[]A = { 1, 1, 2, 3, 3 };``    ``int` `N = A.Length;``    ``CalcUnique(A, N);``  ``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

`2`

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

#### Another Approach: Using Built-in Python Functions:

• Calculate the frequencies using Counter() function.
• Traverse the array and find the element with frequency 1 and print it.

Below is the implementation:

## Python3

 `# Python 3 implementation of the``# above approach``from` `collections ``import` `Counter` `# Function call to find``# element in A[] with frequency = 1``def` `CalcUnique(A, N):` `    ``# Calculate frequency of``    ``# all array elements``    ``freq ``=` `Counter(A)``    ` `    ``# Traverse the Array``    ``for` `i ``in` `A:` `        ``# If non-repeating element``        ``# is found``        ``if` `(freq[i] ``=``=` `1``):``            ``print``(i)``            ``return`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``A ``=` `[``1``, ``1``, ``2``, ``3``, ``3``]``    ``N ``=` `len``(A)``    ``CalcUnique(A, N)` `# This code is contributed by vikkycirus`

Output:

`2`

Time Complexity: O(N)

Auxiliary Space: O(N)

My Personal Notes arrow_drop_up