Java Program to Print All Non Repeated Elements in an Array

This is the Java Program to Find the Elements that do Not have Duplicates.

Problem Description

Given an array, print all the elements whose frequency is one, that is they do not have duplicates.

Example:
Array = [-1, -2, 3, 3, -2]

Output = -1

Problem Solution

Traverse through the array and find the frequency of each element, if the frequency is one, then print the element.

Program/Source Code
advertisement
advertisement

Here is the source code of the Java Program to Find the Elements that do Not have Duplicates. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Find the Elements that do Not have Duplicates
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. public class NoDuplicates {
  7.     // Function to print elements with no duplicates
  8.     static void printElementsWithNoDuplicates(int[] array){
  9.         int i,j;
  10.         int count;
  11.         int x = 0;
  12.         boolean[] flag = new boolean[array.length];
  13.         for(i=0; i<array.length; i++){
  14.             if(!flag[i]){
  15.                 count  = 1;
  16.                 for(j=i+1; j<array.length;j++){
  17.                     if(array[j] == array[i])
  18.                     {
  19.                         count++;
  20.                         flag[j] = true;
  21.                     }
  22.                 }
  23.                 if(count == 1){
  24.                     System.out.println(array[i]);
  25.                     x++;
  26.                 }
  27.             }
  28.         }
  29.         if(x==0){
  30.             System.out.println("All elements are repeated");
  31.         }
  32.     }
  33.     // Function to read input
  34.     public static void main(String[] args) {
  35.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  36.         int size;
  37.         System.out.println("Enter the size of the array");
  38.         try {
  39.             size = Integer.parseInt(br.readLine());
  40.         } catch (Exception e) {
  41.             System.out.println("Invalid Input");
  42.             return;
  43.         }
  44.         int[] array = new int[size];
  45.         System.out.println("Enter array elements");
  46.         int i;
  47.         for (i = 0; i < array.length; i++) {
  48.             try {
  49.                 array[i] = Integer.parseInt(br.readLine());
  50.             } catch (Exception e) {
  51.                 System.out.println("An error Occurred");
  52.             }
  53.         }
  54.         System.out.println("The elements are ");
  55.         printElementsWithNoDuplicates(array);
  56.     }
  57. }
Program Explanation

1. In function printElementsWithNoDuplicates(), an array of boolean is created to keep track of the elements which are visited.
2. The loop for(i=0; i<array.length; i++) is used to iterate through the array.
3. The condition if(!flag[i]) is used to check whether the element is not visited already.
4. If true, then the nested loop for(j=i+1; j<array.length;j++) is used to find the frequency of the element.
5. If the frequency is 1, then the element is printed.
6. If every element has a duplicate, then a suitable message is displayed.

Note: Join free Sanfoundry classes at Telegram or Youtube

Time Complexity: O(n2) where n is the number of elements in the array.

Runtime Test Cases
 
Case 1 (Simple Test Case - some elements are unique):
 
Enter the size of the array
6
Enter array elements
1
2
3
4
5
5
The elements are 
1
2
3
4
 
Case 2 (Simple Test Case - all elements have a duplicate):
 
Enter the size of the array
6
Enter array elements
1
2
3
3
2
1
The elements are 
All elements are repeated

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.