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:
- Sort the given array in non-decreasing order using any sorting algorithm.
- Traverse the sorted array and compare each element with its adjacent element.
- If an element is not equal to its adjacent elements, then print it.
- For the first element, check if it is different from the second element. If yes, print it.
- 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++
<?php
function occurredOnce(& $arr , $n )
{
sort( $arr );
if ( $arr [0] != $arr [1])
echo $arr [0]. " " ;
for ( $i = 1; $i < $n - 1; $i ++)
if ( $arr [ $i ] != $arr [ $i + 1] &&
$arr [ $i ] != $arr [ $i - 1])
echo $arr [ $i ]. " " ;
if ( $arr [ $n - 2] != $arr [ $n - 1])
echo $arr [ $n - 1]. " " ;
}
$arr = array (7, 7, 8, 8, 9,
1, 1, 4, 2, 2);
$n = sizeof( $arr );
occurredOnce( $arr , $n );
?>
|
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++
<?php
function occurredOnce(& $arr , $n )
{
$i = 1;
$len = $n ;
if ( $arr [0] == $arr [ $len - 1])
{
$i = 2;
$len --;
}
for (; $i < $n ; $i ++)
if ( $arr [ $i ] == $arr [ $i - 1])
$i ++;
else
echo $arr [ $i - 1] . " " ;
if ( $arr [ $n - 1] != $arr [0] &&
$arr [ $n - 1] != $arr [ $n - 2])
echo $arr [ $n - 1];
}
$arr = array (7, 7, 8, 8, 9,
1, 1, 4, 2, 2);
$n = sizeof( $arr );
occurredOnce( $arr , $n );
?>
|
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++
#include<bits/stdc++.h>
using namespace std;
void OccurredOnce( int arr[], int n) {
unordered_map< int , int > mp;
for ( int i=0;i<n;i++){
mp[arr[i]]++;
}
for ( auto item: mp){
if (item.second == 1) {
cout<<item.first<< " " ;
}
}
}
int main() {
int arr[] = {-9, -8, 4, 4, 5, 5, -1};
int n = sizeof (arr)/ sizeof (arr[0]);
OccurredOnce(arr, n);
return 0;
}
|
Java
import java.util.HashMap;
public class Main {
public static void OccurredOnce( int [] arr) {
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 );
}
}
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
from collections import Counter
def occurredOnce(arr, n):
mp = Counter(arr)
for it in mp:
if mp[it] = = 1 :
print (it, end = " " )
arr = [ 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 ]
n = len (arr)
occurredOnce(arr, n)
|
Javascript
function occurredOnce(arr, n) {
let mp = new Map();
for (let i=0; i<n; i++) {
mp.set(arr[i], (mp.get(arr[i]) || 0) + 1);
}
for (let [key, value] of mp) {
if (value == 1) {
console.log(key + " " );
}
}
}
let arr = [-9, -8, 4, 4, 5, 5, -1];
let n = arr.length;
occurredOnce(arr, n);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void OccurredOnce( int [] arr, int n) {
Dictionary< int , int > mp = arr.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
foreach ( var item in mp) {
if (item.Value == 1) {
Console.Write(item.Key + " " );
}
}
}
static void Main( string [] args) {
int [] arr = {7, 7, 8, 8, 9, 1, 1, 4, 2, 2};
int n = arr.Length;
OccurredOnce(arr, n);
}
}
|
Time Complexity: O(n) Auxiliary Space: O(n)
|