Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Hash
  • Practice Hash
  • MCQs on Hash
  • Hashing Tutorial
  • Hash Function
  • Index Mapping
  • Collision Resolution
  • Open Addressing
  • Separate Chaining
  • Quadratic probing
  • Double Hashing
  • Load Factor and Rehashing
  • Advantage & Disadvantage
Open In App
Next Article:
Find any one of the multiple repeating elements in read only array
Next article icon

Elements that occurred only once in the array

Last Updated : 01 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr that has numbers appearing twice or once. The task is to identify numbers that occur only once in the array. 

Note: Duplicates appear side by side every time. There might be a few numbers that can occur at one time and just assume this is a right rotating array (just say an array can rotate k times towards right). The order of the elements in the output doesn’t matter.

Examples: 

Input: arr[] = { 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 } Output: 9 4  Input: arr[] = {-9, -8, 4, 4, 5, 5, -1} Output: -9 -8 -1

Method-1: Using Sorting. 

  • Sort the array.
  • Check for each element at index i (except the first and last element), if
arr[i] != arr[i-1] && arr [i] != arr[i+1]
  • For the first element, check if arr[0] != arr[1].
  • For the last element, check if arr[n-1] != arr[n-2].

Algorithm:

  1. Sort the given array in non-decreasing order using any sorting algorithm.
  2. Traverse the sorted array and compare each element with its adjacent element.
  3. If an element is not equal to its adjacent elements, then print it.
  4. For the first element, check if it is different from the second element. If yes, print it.
  5. For the last element, check if it is different from the second last element. If yes, print it.
     

Pseudocode:

occurredOnce(arr[], n):  sort(arr, arr + n)  for i = 0 to n-1 do  if i == 0 and arr[i] != arr[i+1] then  print arr[i]  else if i == n-1 and arr[i] != arr[i-1] then  print arr[i]  else if arr[i] != arr[i-1] and arr[i] != arr[i+1] then  print arr[i]

Below is the implementation of the above approach:

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the elements that
// appeared only once in the array
void occurredOnce(int arr[], int n)
{
    // Sort the array
    sort(arr, arr + n);
 
    // Check for first element
    if (arr[0] != arr[1])
        cout << arr[0] << " ";
 
    // Check for all the elements if it is different
    // its adjacent elements
    for (int i = 1; i < n - 1; i++)
        if (arr[i] != arr[i + 1] && arr[i] != arr[i - 1])
            cout << arr[i] << " ";
 
    // Check for the last element
    if (arr[n - 2] != arr[n - 1])
        cout << arr[n - 1] << " ";
}
 
// Driver code
int main()
{
 
    int arr[] = { 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    occurredOnce(arr, n);
 
    return 0;
}
 
 

Java




// Java implementation
// of above approach
import java.util.*;
 
class GFG
{
 
// Function to find the elements that
// appeared only once in the array
static void occurredOnce(int arr[], int n)
{
    // Sort the array
    Arrays.sort(arr);
 
    // Check for first element
    if (arr[0] != arr[1])
        System.out.println(arr[0] + " ");
 
    // Check for all the elements
    // if it is different
    // its adjacent elements
    for (int i = 1; i < n - 1; i++)
        if (arr[i] != arr[i + 1] &&
            arr[i] != arr[i - 1])
            System.out.print(arr[i] + " ");
 
    // Check for the last element
    if (arr[n - 2] != arr[n - 1])
        System.out.print(arr[n - 1] + " ");
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = {7, 7, 8, 8, 9,
                 1, 1, 4, 2, 2};
    int n = arr.length;
 
    occurredOnce(arr, n);
}
}
 
// This code is contributed
// by Arnab Kundu
 
 

Python3




# Python 3 implementation
# of above approach
 
# Function to find the elements
# that appeared only once in
# the array
def occurredOnce(arr, n):
     
    # Sort the array
    arr.sort()
 
    # Check for first element
    if arr[0] != arr[1]:
        print(arr[0], end = " ")
 
    # Check for all the elements
    # if it is different its
    # adjacent elements
    for i in range(1, n - 1):
        if (arr[i] != arr[i + 1] and
            arr[i] != arr[i - 1]):
            print( arr[i], end = " ")
 
    # Check for the last element
    if arr[n - 2] != arr[n - 1]:
        print(arr[n - 1], end = " ")
 
# Driver code
if __name__ == "__main__":
 
    arr = [ 7, 7, 8, 8, 9,
            1, 1, 4, 2, 2 ]
    n = len(arr)
    occurredOnce(arr, n)
 
# This code is contributed
# by ChitraNayal
 
 

Javascript




<script>
 
// Javascript implementation
// of above approach
 
// Function to find the elements that
// appeared only once in the array
function occurredOnce(arr,n)
{
    // Sort the array
    arr.sort(function(a,b){return a-b;});
  
    // Check for first element
    if (arr[0] != arr[1])
        document.write(arr[0] + " ");
  
    // Check for all the elements
    // if it is different
    // its adjacent elements
    for (let i = 1; i < n - 1; i++)
        if (arr[i] != arr[i + 1] &&
            arr[i] != arr[i - 1])
            document.write(arr[i] + " ");
  
    // Check for the last element
    if (arr[n - 2] != arr[n - 1])
        document.write(arr[n - 1] + " ");
}
 
// Driver code
let arr=[7, 7, 8, 8, 9,
                 1, 1, 4, 2, 2];
let n = arr.length;
occurredOnce(arr, n);
 
 
// This code is contributed by rag2127
 
</script>
 
 

C#




// C# implementation
// of above approach
using System;
 
class GFG
{
 
// Function to find the elements that
// appeared only once in the array
static void occurredOnce(int[] arr, int n)
{
    // Sort the array
    Array.Sort(arr);
 
    // Check for first element
    if (arr[0] != arr[1])
        Console.Write(arr[0] + " ");
 
    // Check for all the elements
    // if it is different
    // its adjacent elements
    for (int i = 1; i < n - 1; i++)
        if (arr[i] != arr[i + 1] &&
            arr[i] != arr[i - 1])
            Console.Write(arr[i] + " ");
 
    // Check for the last element
    if (arr[n - 2] != arr[n - 1])
        Console.Write(arr[n - 1] + " ");
}
 
// Driver code
public static void Main()
{
    int[] arr = {7, 7, 8, 8, 9,
                1, 1, 4, 2, 2};
    int n = arr.Length;
 
    occurredOnce(arr, n);
}
}
 
// This code is contributed
// by ChitraNayal
 
 

PHP




<?php
// PHP implementation
// of above approach
 
// Function to find the elements
// that appeared only once in
// the array
function occurredOnce(&$arr, $n)
{
    // Sort the array
    sort($arr);
 
    // Check for first element
    if ($arr[0] != $arr[1])
        echo $arr[0]." ";
 
    // Check for all the elements
    // if it is different its
    // adjacent elements
    for ($i = 1; $i < $n - 1; $i++)
        if ($arr[$i] != $arr[$i + 1] &&
            $arr[$i] != $arr[$i - 1])
            echo $arr[$i]." ";
 
    // Check for the last element
    if ($arr[$n - 2] != $arr[$n - 1])
        echo $arr[$n - 1]." ";
}
 
// Driver code
$arr = array(7, 7, 8, 8, 9,
             1, 1, 4, 2, 2);
$n = sizeof($arr);
occurredOnce($arr, $n);
 
// This code is contributed
// by ChitraNayal
?>
 
 
Output
4 9 

Complexity Analysis:

  • Time Complexity: O(Nlogn) 
  • Auxiliary Space: O(1)

Method-2: (Using Hashing): In C++, unordered_map can be used for hashing. 

  • Traverse the array.
  • Store each element with its occurrence in the unordered_map.
  • Traverse the unordered_map and print all the elements with occurrence 1.

Below is the implementation of the above approach: 

C++




// C++ implementation to find elements
// that appeared only once
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the elements that
// appeared only once in the array
void occurredOnce(int arr[], int n)
{
    unordered_map<int, int> mp;
 
    // Store all the elements in the map with
    // their occurrence
    for (int i = 0; i < n; i++)
        mp[arr[i]]++;
 
    // Traverse the map and print all the
    // elements with occurrence 1
    for (auto it = mp.begin(); it != mp.end(); it++)
        if (it->second == 1)
            cout << it->first << " ";
}
 
// Driver code
int main()
{
 
    int arr[] = { 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    occurredOnce(arr, n);
 
    return 0;
}
 
 

Java




// Java implementation to find elements
// that appeared only once
import java.util.*;
import java.io.*;
 
class GFG
{
    // Function to find the elements that
    // appeared only once in the array
    static void occurredOnce(int[] arr, int n)
    {
            HashMap<Integer, Integer> mp = new HashMap<>();
             
            // Store all the elements in the map with
            // their occurrence
            for (int i = 0; i < n; i++)
            {
                if (mp.containsKey(arr[i]))
                    mp.put(arr[i], 1 + mp.get(arr[i]));
                else
                    mp.put(arr[i], 1);
            }
 
            // Traverse the map and print all the
            // elements with occurrence 1
            for (Map.Entry entry : mp.entrySet())
            {
                if (Integer.parseInt(String.valueOf(entry.getValue())) == 1)
                    System.out.print(entry.getKey() + " ");
            }
    }
 
    // Driver code
    public static void main(String args[])
    {
            int[] arr = { 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 };
            int n = arr.length;
 
            occurredOnce(arr, n);
    }
}
 
// This code is contributed by rachana soma
 
 

Python3




# Python3 implementation to find elements
# that appeared only once
import math as mt
 
# Function to find the elements that
# appeared only once in the array
def occurredOnce(arr, n):
 
    mp = dict()
 
    # Store all the elements in the
    # map with their occurrence
    for i in range(n):
        if arr[i] in mp.keys():
            mp[arr[i]] += 1
        else:
            mp[arr[i]] = 1
 
    # Traverse the map and print all
    # the elements with occurrence 1
    for it in mp:
        if mp[it] == 1:
            print(it, end = " ")
 
# Driver code
arr = [7, 7, 8, 8, 9, 1, 1, 4, 2, 2]
n = len(arr)
 
occurredOnce(arr, n)
 
# This code is contributed by
# Mohit Kumar 29
 
 

Javascript




<script>
 
// Javascript implementation to find elements
// that appeared only once
     
// Function to find the elements that
// appeared only once in the array
function occurredOnce(arr, n)
{
    let mp = new Map();
     
    // Store all the elements in the map
    // with their occurrence
    for(let i = 0; i < n; i++)
    {
        if (mp.has(arr[i]))
            mp.set(arr[i], 1 + mp.get(arr[i]));
        else
            mp.set(arr[i], 1);
    }
 
    // Traverse the map and print all the
    // elements with occurrence 1
    for(let [key, value] of mp.entries())
    {
        if (value == 1)
            document.write(key + " ");
    }
}
 
// Driver code
let arr = [ 7, 7, 8, 8, 9,
            1, 1, 4, 2, 2 ];
let n = arr.length;
 
occurredOnce(arr, n);
 
// This code is contributed by avanitrachhadiya2155
 
</script>
 
 

C#




// C# implementation to find elements
// that appeared only once
using System;
using System.Collections.Generic;
class GFG
{
    // Function to find the elements that
    // appeared only once in the array
    static void occurredOnce(int[] arr, int n)
    {
      Dictionary<int, int> mp = new Dictionary<int, int>();
 
      // Store all the elements in the map with
      // their occurrence
      for (int i = 0; i < n; i++)
      {
        if (mp.ContainsKey(arr[i]))
          mp[arr[i]] = 1 + mp[arr[i]];
        else
          mp.Add(arr[i], 1);
      }
 
      // Traverse the map and print all the
      // elements with occurrence 1
      foreach(KeyValuePair<int, int> entry in mp)
      {
        if (Int32.Parse(String.Join("", entry.Value)) == 1)
          Console.Write(entry.Key + " ");
      }
    }
 
    // Driver code
    public static void Main(String []args)
    {
      int[] arr = { 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 };
      int n = arr.Length;
      occurredOnce(arr, n);
    }
}
 
// This code is contributed by shikhasingrajput
 
 
Output
4 9 

Complexity Analysis:

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

Method-3: Using given assumptions.

It is given that an array can be rotated any time and duplicates will appear side by side every time. So, after rotating, the first and last elements will appear side by side. 

  • Check if the first and last elements are equal. If yes, then start traversing the elements between them.
  • Check if the current element is equal to the element in the immediate previous index. If yes, check the same for the next element.
  • If not, print the current element.

Implementation:

C++




// C++ implementation to find elements
// that appeared only once
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the elements that
// appeared only once in the array
void occurredOnce(int arr[], int n)
{
    int i = 1, len = n;
 
    // Check if the first and last element is equal
    // If yes, remove those elements
    if (arr[0] == arr[len - 1]) {
        i = 2;
        len--;
    }
 
    // Start traversing the remaining elements
    for (; i < n; i++)
 
        // Check if current element is equal to
        // the element at immediate previous index
        // If yes, check the same for next element
        if (arr[i] == arr[i - 1])
            i++;
 
        // Else print the current element
        else
            cout << arr[i - 1] << " ";
 
    // Check for the last element
    if (arr[n - 1] != arr[0] && arr[n - 1] != arr[n - 2])
        cout << arr[n - 1];
}
 
// Driver code
int main()
{
 
    int arr[] = { 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    occurredOnce(arr, n);
 
    return 0;
}
 
 

Java




// Java implementation to find
// elements that appeared only once
class GFG
{
// Function to find the elements that
// appeared only once in the array
static void occurredOnce(int arr[], int n)
{
    int i = 1, len = n;
 
    // Check if the first and last
    // element is equal. If yes,
    // remove those elements
    if (arr[0] == arr[len - 1])
    {
        i = 2;
        len--;
    }
 
    // Start traversing the
    // remaining elements
    for (; i < n; i++)
 
        // Check if current element is
        // equal to the element at
        // immediate previous index
        // If yes, check the same
        // for next element
        if (arr[i] == arr[i - 1])
            i++;
 
        // Else print the current element
        else
            System.out.print(arr[i - 1] + " ");
 
    // Check for the last element
    if (arr[n - 1] != arr[0] &&
        arr[n - 1] != arr[n - 2])
        System.out.print(arr[n - 1]);
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = {7, 7, 8, 8, 9,
                 1, 1, 4, 2, 2};
    int n = arr.length;
 
    occurredOnce(arr, n);
}
}
 
// This code is contributed
// by Arnab Kundu
 
 

Python3




# Python 3 implementation to find
# elements that appeared only once
 
# Function to find the elements that
# appeared only once in the array
def occurredOnce(arr, n):
    i = 1
    len = n
 
    # Check if the first and
    # last element is equal
    # If yes, remove those elements
    if arr[0] == arr[len - 1]:
        i = 2
        len -= 1
 
    # Start traversing the
    # remaining elements
    while i < n:
 
        # Check if current element is
        # equal to the element at
        # immediate previous index
        # If yes, check the same for
        # next element
        if arr[i] == arr[i - 1]:
            i += 1
 
        # Else print the current element
        else:
            print(arr[i - 1], end = " ")
             
        i += 1
 
    # Check for the last element
    if (arr[n - 1] != arr[0] and
        arr[n - 1] != arr[n - 2]):
        print(arr[n - 1])
 
# Driver code
if __name__ == "__main__":
    arr = [ 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 ]
    n = len(arr)
 
    occurredOnce(arr, n)
 
# This code is contributed
# by ChitraNayal
 
 

Javascript




<script>
 
// Javascript implementation to find
// elements that appeared only once
 
// Function to find the elements that
// appeared only once in the array
function occurredOnce(arr, n)
{
    var i = 1, len = n;
 
    // Check if the first and last
    // element is equal. If yes,
    // remove those elements
    if (arr[0] == arr[len - 1])
    {
        i = 2;
        len--;
    }
 
    // Start traversing the
    // remaining elements
    for(; i < n; i++)
 
        // Check if current element is
        // equal to the element at
        // immediate previous index
        // If yes, check the same
        // for next element
        if (arr[i] == arr[i - 1])
            i++;
 
        // Else print the current element
        else
            document.write(arr[i - 1] + " ");
 
    // Check for the last element
    if (arr[n - 1] != arr[0] &&
        arr[n - 1] != arr[n - 2])
        document.write(arr[n - 1]);
}
 
// Driver code
var arr = [ 7, 7, 8, 8, 9,
            1, 1, 4, 2, 2 ];
var n = arr.length;
 
occurredOnce(arr, n);
 
// This code is contributed by Ankita saini
 
</script>
 
 

C#




// C# implementation to find
// elements that appeared only once
using System;
 
class GFG
{
// Function to find the elements that
// appeared only once in the array
static void occurredOnce(int[] arr, int n)
{
    int i = 1, len = n;
 
    // Check if the first and last
    // element is equal. If yes,
    // remove those elements
    if (arr[0] == arr[len - 1])
    {
        i = 2;
        len--;
    }
 
    // Start traversing the
    // remaining elements
    for (; i < n; i++)
 
        // Check if current element is
        // equal to the element at
        // immediate previous index
        // If yes, check the same
        // for next element
        if (arr[i] == arr[i - 1])
            i++;
 
        // Else print the current element
        else
            Console.Write(arr[i - 1] + " ");
 
    // Check for the last element
    if (arr[n - 1] != arr[0] &&
        arr[n - 1] != arr[n - 2])
        Console.Write(arr[n - 1]);
}
 
// Driver code
public static void Main()
{
    int[] arr = {7, 7, 8, 8, 9,
                1, 1, 4, 2, 2};
    int n = arr.Length;
 
    occurredOnce(arr, n);
}
}
 
// This code is contributed
// by ChitraNayal
 
 

PHP




<?php
// PHP implementation to find
// elements that appeared only once
 
// Function to find the elements that
// appeared only once in the array
function occurredOnce(&$arr, $n)
{
    $i = 1;
    $len = $n;
 
    // Check if the first and last
    // element is equal. If yes,
    // remove those elements
    if ($arr[0] == $arr[$len - 1])
    {
        $i = 2;
        $len--;
    }
 
    // Start traversing the
    // remaining elements
    for (; $i < $n; $i++)
 
        // Check if current element is
        // equal to the element at
        // immediate previous index
        // If yes, check the same for
        // next element
        if ($arr[$i] == $arr[$i - 1])
            $i++;
 
        // Else print the current element
        else
            echo $arr[$i - 1] . " ";
 
    // Check for the last element
    if ($arr[$n - 1] != $arr[0] &&
        $arr[$n - 1] != $arr[$n - 2])
        echo $arr[$n - 1];
}
 
// Driver code
$arr = array(7, 7, 8, 8, 9,
             1, 1, 4, 2, 2);
$n = sizeof($arr);
 
occurredOnce($arr, $n);
 
// This code is contributed
// by ChitraNayal
?>
 
 
Output
9 4 

Complexity Analysis:

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

Method #4:Using built-in Python functions:

  • Count the frequencies of every element using the Counter function
  • Traverse the frequency array and print all the elements with occurrence 1.

Below is the implementation

C++




// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to find the elements that
// appeared only once in the array
void OccurredOnce(int arr[], int n) {
    // counting frequency of every element
    // using unordered_map
    unordered_map<int, int> mp;
    for(int i=0;i<n;i++){
        mp[arr[i]]++;
    }
 
    // Traverse the map and print all
    // the elements with occurrence 1
    for(auto item: mp){
        if (item.second == 1) {
            cout<<item.first<<" ";
        }
    }
}
 
// Driver code
int main() {
    int arr[] = {-9, -8, 4, 4, 5, 5, -1};
    int n = sizeof(arr)/sizeof(arr[0]);
    OccurredOnce(arr, n);
    return 0;
}
 
// This code is contributed by adityashatmfh
 
 

Java




import java.util.HashMap;
 
public class Main {
    public static void OccurredOnce(int[] arr) {
        // Counting frequency of every element using HashMap
        HashMap<Integer, Integer> mp = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            if (mp.containsKey(arr[i])) {
                mp.put(arr[i], mp.get(arr[i]) + 1);
            } else {
                mp.put(arr[i], 1);
            }
        }
        // Traverse the map and print all the elements with occurrence 1
        for (int it : mp.keySet()) {
            if (mp.get(it) == 1) {
                System.out.print(it + " ");
            }
        }
    }
 
    public static void main(String[] args) {
        int[] arr = {7, 7, 8, 8, 9, 1, 1, 4, 2, 2};
        OccurredOnce(arr);
    }
}
 
 

Python3




# Python3 implementation to find elements
# that appeared only once
from collections import Counter
 
# Function to find the elements that
# appeared only once in the array
def occurredOnce(arr, n):
 
    #counting frequency of every element using Counter
    mp=Counter(arr)
    # Traverse the map and print all
    # the elements with occurrence 1
    for it in mp:
        if mp[it] == 1:
            print(it, end = " ")
 
# Driver code
arr = [7, 7, 8, 8, 9, 1, 1, 4, 2, 2]
n = len(arr)
 
occurredOnce(arr, n)
 
# This code is contributed by vikkycirus
 
 

Javascript




function occurredOnce(arr, n) {
// counting frequency of every element
// using Map
let mp = new Map();
for(let i=0; i<n; i++) {
mp.set(arr[i], (mp.get(arr[i]) || 0) + 1);
}
 
// Traverse the map and print all
// the elements with occurrence 1
for(let [key, value] of mp) {
if(value == 1) {
console.log(key + " ");
}
}
}
 
// Driver code
let arr = [-9, -8, 4, 4, 5, 5, -1];
let n = arr.length;
occurredOnce(arr, n);
 
 

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class Program {
     
    // Function to find the elements that
    // appeared only once in the array
    static void OccurredOnce(int[] arr, int n) {
        // counting frequency of every element using .Count() method
        Dictionary<int, int> mp = arr.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
         
        // Traverse the map and print all
        // the elements with occurrence 1
        foreach (var item in mp) {
            if (item.Value == 1) {
                Console.Write(item.Key + " ");
            }
        }
    }
     
    // Driver code
    static void Main(string[] args) {
        int[] arr = {7, 7, 8, 8, 9, 1, 1, 4, 2, 2};
        int n = arr.Length;
        OccurredOnce(arr, n);
    }
}
 
// This code is contributed by Prince Kumar
 
 
Output
9 4 

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



Next Article
Find any one of the multiple repeating elements in read only array
author
namankedia
Improve
Article Tags :
  • Arrays
  • DSA
  • Hash
  • Sorting
  • Amazon
  • cpp-unordered_map
  • rotation
Practice Tags :
  • Amazon
  • Arrays
  • Hash
  • Sorting

Similar Reads

  • Array elements that appear more than once
    Given an integer array, print all repeating elements (Elements that appear more than once) in the array. The output should contain elements according to their first occurrences. Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}Output: 10 45 Input: arr[] = {1, 2, 3, 4, 2, 5}Output:2 Input: arr[
    15+ min read
  • Find the only non-repeating element in a given array
    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: 2Explanation: Distinct array elements are {1,
    11 min read
  • Remove All Occurrences of an Element in an Array
    Given an integer array arr[] and an integer ele the task is to the remove all occurrences of ele from arr[] in-place and return the number of elements which are not equal to ele. If there are k number of elements which are not equal to ele then the input array arr[] should be modified such that the
    6 min read
  • Find any one of the multiple repeating elements in read only array
    Given a read-only array of size ( n+1 ), find one of the multiple repeating elements in the array where the array contains integers only between 1 and n. A read-only array means that the contents of the array can’t be modified.Examples: Input : n = 5 arr[] = {1, 1, 2, 3, 5, 4} Output : One of the nu
    15 min read
  • First element occurring k times in an array
    Given an array arr[] of size n, the task is to find the first element that occurs k times. If no element occurs k times, print -1. If multiple elements occur k times, the first one in the array should be the answer. Examples: Input: arr = [1,7,4,3,4,8,7], k=2Output: 7Explanation: Both 7 and 4 occur
    9 min read
  • Find the only different element in an array
    Given an array of integers where all elements are same except one element, find the only different element in the array. It may be assumed that the size of the array is at least two.Examples: Input : arr[] = {10, 10, 10, 20, 10, 10} Output : 3 arr[3] is the only different element.Input : arr[] = {30
    10 min read
  • Find the only element that appears b times
    Given an array where every element occurs 'a' times, except one element which occurs b (a>b) times. Find the element that occurs b times. Examples: Input : arr[] = [1, 1, 2, 2, 2, 3, 3, 3] a = 3, b = 2 Output : 1 Add each number once and multiply the sum by a, we will get a times the sum of each
    11 min read
  • Find the Most Frequent Element in an Array in PHP
    Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1,
    2 min read
  • Count occurrences of the average of array elements with a given number
    Given an array of [Tex]N [/Tex]integers and an integer [Tex]x [/Tex]. For every integer of the array a[i], the task is to calculate the count of numbers in the array with value equals to the average of element a[i] and x. That is, the number of occurrences of the (average of element a[i] and x) in t
    7 min read
  • Find the two repeating elements in a given array
    Given an array arr[] of N+2 elements. All elements of the array are in the range of 1 to N. And all elements occur once except two numbers which occur twice. Find the two repeating numbers. Examples: Input: arr = [4, 2, 4, 5, 2, 3, 1], N = 5Output: 4 2Explanation: The above array has n + 2 = 7 eleme
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences