- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to print all distinct elements of a given integer array in Java
All distinct elements of an array are printed i.e. all the elements in the array are printed only once and duplicate elements are not printed. An example of this is given as follows.
Array = 1 5 9 1 4 9 6 5 9 7 Distinct elements of above array = 1 5 9 4 6 7
A program that demonstrates this is given as follows.
Example
public class Example { public static void main (String[] args) { int arr[] = {1, 5, 9, 1, 4, 9, 6, 5, 9, 7}; int n = arr.length; int i, j; System.out.print("The array is: "); for (i = 0; i < n; ++i) System.out.print(arr[i] + " "); System.out.print("
The distinct elements of above array are: "); for (i = 0; i < n; i++) { for (j = 0; j < i; j++) if (arr[i] == arr[j]) break; if (i == j) System.out.print( arr[i] + " "); } } }
Output
The array is: 1 5 9 1 4 9 6 5 9 7 The distinct elements of above array are: 1 5 9 4 6 7
Now let us understand the above program.
First the original array is displayed. This array may contain duplicate elements. The code snippet that demonstrates this is given as follows −
System.out.print("The array is: "); for (i = 0; i < n; ++i) System.out.print(arr[i] + " ");
Now, a nested for loop is used to make sure only distinct elements of the array are displayed. The outer loop runs from 0 to n and the inner loop makes sure that an element is printed only if has not occured before. The code snippet that demonstrates this is given as follows −
System.out.print("
The distinct elements of above array are: "); for (i = 0; i < n; i++) { for (j = 0; j < i; j++) if (arr[i] == arr[j]) break; if (i == j) System.out.print( arr[i] + " "); }
- Related Articles
- Python program to print all distinct elements of a given integer array.
- C# program to print all distinct elements of a given integer array in C#
- Print All Distinct Elements of a given integer array in C++
- Java Program to Print a Square Pattern for given integer
- Java Program to print distinct permutations of a string
- Java Program to Print the Elements of an Array
- Java Program to Print an Integer
- Java Program to generate random elements from a given array
- JAVA Program to Replace Element of Integer Array with Product of Other Elements
- Java Program to Print Boundary Elements of a Matrix
- Program to convert set of Integer to Array of Integer in Java
- Print sorted distinct elements of array in C language
- Python Program to print all distinct uncommon digits present in two given numbers
- Java program to print a given pattern. (stars)
- C# program to find all duplicate elements in an integer array

Advertisements