How to get distinct elements from an array by avoiding duplicate elements?
Description:
The below example shows how to avoid duplicate elements from an array and disply only distinct elements. Please use only arrays to process it.
Code:
package com.java2novice.algos;
public class MyDisticntElements {
public static void printDistinctElements(int[] arr){
for(int i=0;i<arr.length;i++){
boolean isDistinct = false;
for(int j=0;j<i;j++){
if(arr[i] == arr[j]){
isDistinct = true;
break;
}
}
if(!isDistinct){
System.out.print(arr[i]+" ");
}
}
}
public static void main(String a[]){
int[] nums = {5,2,7,2,4,7,8,2,3};
MyDisticntElements.printDistinctElements(nums);
}
}
Output:
5 2 7 4 8 3
Description:
The below example shows how to avoid duplicate elements from an array and disply only distinct elements. Please use only arrays to process it.
Code:
package com.java2novice.algos;
public class MyDisticntElements {
public static void printDistinctElements(int[] arr){
for(int i=0;i<arr.length;i++){
boolean isDistinct = false;
for(int j=0;j<i;j++){
if(arr[i] == arr[j]){
isDistinct = true;
break;
}
}
if(!isDistinct){
System.out.print(arr[i]+" ");
}
}
}
public static void main(String a[]){
int[] nums = {5,2,7,2,4,7,8,2,3};
MyDisticntElements.printDistinctElements(nums);
}
}
Output:
5 2 7 4 8 3
To get distinct elements from an array and avoid duplicates in Java, you can use a
HashSet
because it does not allow duplicate elements. Here’s a simple program that demonstrates this:import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class DistinctElements {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] array = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
scanner.close();
int[] distinctArray = getDistinctElements(array);
System.out.println("Distinct elements in the array: " + Arrays.toString(distinctArray));
}
public static int[] getDistinctElements(int[] array) {
Set<Integer> set = new HashSet<>();
for (int element : array) {
set.add(element);
}
int[] distinctArray = new int[set.size()];
int index = 0;
for (int element : set) {
distinctArray[index++] = element;
}
return distinctArray;
}
}
Explanation:
- Import Necessary Classes: The program uses
Scanner
for input,HashSet
for storing unique elements, andArrays
for output formatting. - Get Input: The user is prompted to enter the number of elements in the array and then the elements themselves.
- getDistinctElements Method: This method converts the array into a
HashSet
to remove duplicates:- Iterate through the array and add each element to the
HashSet
. - Convert the
HashSet
back into an array.
- Iterate through the array and add each element to the
- Output: The distinct elements are printed.
This program reads an array from the user, removes duplicates using a HashSet
, and prints the array of distinct elements.