Program to find non repeating element in an array (Unique elements)

Finding the non repeating element in an array can be done in 2 different ways.

Method 1: Use two loops, one for the current element and the other to check if the element is already present in the array or not.

Method 2: Traverse the array and insert the array elements and their number of occurences in the hash table. Traverse the array again and print the array elements with count = 1.

Method 1 to find non repeating element in an array

An easier approach to find all non-repeating element in an array is to use two loops. Use the first loop for traversing the array and the second loop to check if the current element is already present in the array or not.

Algorithm

  • Declare the array and input the array elements.
  • Start traversing the array and check if the current element is already present in the array.
  • If it is already present in the array, move to the next element in the array and continue.
  • If not, output the element as the non-repeating element.
#include <stdio.h>
#include <stdlib.h>
int non_repeating_elements(int arr[], int n)
{
int i,j;
int count = 1;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(arr[i] == arr[j] && i != j)
break;
}
if(j == n )
{
printf("\nNon-repeating element [%d] : %d \n",count,arr[i]);
++count;
}
}
return -1;
}
int main()
{
int n,i;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
int arr[n];
printf("\nInput the array elements : ");
for(i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
non_repeating_elements(arr, n);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int distinct(int*,int);
int i;
int main()
{
int n,*a;
scanf("%d",&n);

for(i=0;i<n;i++)
scanf("%d",(a+i));

printf("There are %d distinct element in the array.",distinct(a,n));
return 0;
}
int distinct(int *a,int n)
{
int max=*(a+i),count=0,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(*(a+i)==*(a+j))
break;
}
if(j==n)
count++;
}
return count;
}
#include <iostream>
using namespace std;
int non_repeating_elements(int arr[], int n)
{
int i,j;
int count = 1;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(arr[i] == arr[j] && i != j)
break;
}
if(j == n )
{
cout << "\nNon-repeating element [" << count << "]" << " : " << arr[i] << endl;
++count;
}
}
return -1;
}
int main()
{
int n,i;
cout << "\nEnter the number of elements : ";
cin >> n;
int arr[n];
cout << "\nInput the array elements : ";
for(i = 0; i < n; i++)
{
cin >> arr[i];
}
non_repeating_elements(arr, n);
return 0;
}

Method 2 for finding non repeating element in an array

The time complexity of this problem can be reduced by using hash tables. The array is traversed and the array elements along with their counts is stored in the hash table. Then the array is traversed again and the elements having their counts = 1 are displayed as non-repeating elements or unique elements in the array.

Algorithm

  • Declare the array and input the array elements.
  • Insert all the array elements in the hash table.
  • Traverse the array again and display the array elements having their count = 1.

#include <iostream>
#include <unordered_map>
using namespace std;

void non_repeating_elements(int arr[], int n)
{
unordered_map<int, int> table;
for (int i = 0; i < n; i++)
table[arr[i]]++;

for (auto e : table)
if (e.second == 1)
cout << e.first << ” “;
}

int main()
{
int n,i;
cout << “\nEnter the number of elements : “;
cin >> n;
int arr[n];
cout << “\nInput the array elements : “;
for(i = 0; i < n; i++)
{
cin >> arr[i];
}

non_repeating_elements(arr, n);
return 0;
}

from collections import defaultdict

def non_repeating_element(arr, n):
      table = defaultdict(lambda:0)

for i in range(n):
      table[arr[i]] += 1

for i in range(n):
      if table[arr[i]] == 1:
             print(arr[i], end = ” “)
return ” “

# Main Function
n = int(input(“Enter the number of elements : “))
print(“Input the array elements : “)
for i in range(0,n):
      arr[i] = int(input())
print(non_repeating_element(arr, n))  

Test case:

Input: 1 2 3 4 1
Output: 2 3 4

non repeating element in an array