Java program to find non-repeating elements in an array

Non Repeating elements in an Array in Java

Here, in this page you will find the code for printing non repeating elements in an array in java programming language We are given with an array and need to print the distinct elements among them. You will found different methods to solve this problem in this page.

Non Repeating elements in an Array in java

Here, we will discuss the following methods in this page to print the distinct elements in the given input array.

  • Method 1 : Using Two loops
  • Method 2 : Using hash Map

Let’s discuss each method one by one,

Method 1 :

In this method we will count the frequency of each elements using two for loops.

  • To check the status of visited elements create a array of size n.
  • Run a loop from index 0 to n and check if (visited[i]==1) then skip that element.
  • Otherwise create a variable count = 1 to keep the count of frequency.
  • Run a loop from index i+1 to n
  • Check if(arr[i]==arr[j]), then increment the count by 1 and set visited[j]=1.
  • After complete iteration of inner for loop and check if count==1, then print that ith element.
Non repeating in java

Method 1 : Code in Java

Run
import java.util.Arrays;

class Main
{
   public static void countFreq(int arr[], int n)
   {
         boolean visited[] = new boolean[n];
         Arrays.fill(visited, false);
         // Traverse through array elements and
         // count frequencies
         for (int i = 0; i < n; i++) {

            // Skip this element if already processed
            if (visited[i] == true)
             continue;

            // Count frequency
            int count = 1;
            for (int j = i + 1; j < n; j++) {
                if (arr[i] == arr[j]) {
                   visited[j] = true;
                   count++;
                }
            }
            if(count==1)   
              System.out.println(arr[i]);
   }
} // Driver code 

public static void main(String []args) 
{ 
int arr[] = new int[]{10, 30, 40, 20, 10, 20, 50, 10}; 
int n = arr.length;
 countFreq(arr, n); 
}
 }

Output

30 
40
50

Method 2 :

In this method we will count use hash-map to count the frequency of each elements.

  • Declare a hash map and a variable count_dis=0.
  • Start iterating over the entire array
  • If element is present in map, then increase the value of frequency by 1.
  • Otherwise, insert that element in map.
  • After complete iteration over array, start traversing map and print those key which have value 1.

Time and Space Complexity :

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

Method 2 : Code in Java

Run
import java.util.*;
import java.util.Arrays;

class Main
{
   static void countFreq(int arr[], int n)
   {
      Map<Integer, Integer> mp = new HashMap<>();
      int count_dis=0;
      // Traverse through array elements and
      // count frequencies
      for (int i = 0; i < n; i++)
      {
         if (mp.containsKey(arr[i]))
         {
           mp.put(arr[i], mp.get(arr[i]) + 1);
         }
         else
         {
           mp.put(arr[i], 1);
         }
     }
     // Traverse through map and print frequencies
     for (Map.Entry<Integer, Integer> entry : mp.entrySet())
     {    if(entry.getValue()==1)
          System.out.println(entry.getKey()+" ");
     }
     
  }
  // Driver code
  public static void main(String []args)
  {
       int arr[] = new int[]{10, 40, 50, 20, 10, 20, 30, 10};
       int n = arr.length;
       countFreq(arr, n);
  }
}

Output

30 
40
50

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

7 comments on “Java program to find non-repeating elements in an array”


  • sagarika

    import java.util.*;
    import java.util.HashMap;
    public class FindNonRepeatingArray {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println(“enter size of array”);
    int n=sc.nextInt();
    int arr[]=new int[n];
    System.out.println(“enter elements of array”);
    for(int i=0;i<n;i++)
    {
    arr[i]=sc.nextInt();
    }
    FindNonRepeating(arr,n);
    }
    static void FindNonRepeating(int arr[],int n)
    {
    HashMapresult=new HashMap();
    for(int i=0;i<n;i++)
    {
    if(result.containsKey(arr[i]))
    {
    result.put(arr[i],result.get(arr[i])+1);
    }
    else
    {
    result.put(arr[i],1);
    }
    }
    for(Map.Entryentry:result.entrySet())
    {
    if(entry.getValue()==1)
    {
    System.out.println(entry.getKey());
    }
    }
    }
    }


  • Lakhan Rathore

    //Finding a Non repeated(Unique) Elements in an Array

    class Lakhan
    {
    public static void main(String ar[])
    {
    int x[]={10,20,30,10,40,20,50,30};
    int n=x.length;
    int f=0;
    for(int i=0;i<n;i++)
    {
    f=0;
    for(int j=0;j<n;j++)
    {
    if(i!=j&&x[i]==x[j])
    {
    f=1;
    break;
    }
    }
    if(f==0)
    System.out.print(" "+x[i]);
    }
    }
    }


  • Gyanendra

    // Using HashMap
    import java.util.*;
    public class Main{
    public static void main(String[] args){
    Scanner scn=new Scanner(System.in);
    int n=scn.nextInt();
    int[] arr=new int[n];

    for(int i=0;i<n;i++){
    arr[i]=scn.nextInt();
    }

    int count=1;

    for(int i=1;i<arr.length;i++){

    if(arr[i]!=-1){

    for(int j=i+1;j<arr.length;j++){
    if(arr[i]==arr[j]){
    count++;
    arr[j]=-1;
    }
    }

    if(count==1){
    System.out.print(arr[i]+" ");

    }
    count=1;
    }
    }
    }
    }


  • Subhajit

    public class NonRepeatingEle {

    public static void nonRepeatElement(int arr[], int n) {
    for (int i = 0; i < n; i++) {
    int count = 0;
    for (int j = i + 1; j < n; j++) {
    if (arr[i] == arr[j] && arr[i] != 0) {
    arr[j] = 0;
    count++;
    }
    }
    if (count == 0 && arr[i]!=0) {
    System.out.print(arr[i] + " ");
    }
    }
    System.out.println();
    }

    public static void main(String[] args) {
    int arr[] = { 10, 30, 40, 20, 10, 30, 65, 74, 65, 20, 50, 10 };
    int n = arr.length;
    nonRepeatElement(arr, n);
    }
    }


  • Mahesh

    int[]a={1,2,42,13,3,55,3,23,55,45,6,25,5,5,53,34,34,33,35};
    Arrays.sort(a);
    boolean []vis=new boolean[a.length];
    for(int i=0;i<a.length-1;i++){
    if(a[i]==a[i+1]){
    vis[i]=true;
    vis[i+1]=true;
    }
    }
    for(int i=0;i<a.length;i++){
    if(!vis[i]){
    System.out.print(a[i]+" ");
    }
    }