In this post of Java program to Remove Duplicate Elements From an Array We will look into three different methods to remove duplicate elements from an array.
You may also like –
Find largest and smallest number in the given Array
Find duplicate elements in an Array
find common elements between two Arrays
- Java program to Remove Duplicate Elements From an Array using Collection APIs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.kkjavatutorials.client; import java.util.HashSet; import java.util.Set; /** * @author KK JavaTutorials * Java program to Remove Duplicate Elements From an Array using Collection APIs */ public class RemoveDuplicateElementsFromArray1 { public static void main(String[] args) { int[] inputArrayWithDupElements = { 5, 8, 5, 50, 26, 26, 100, 1001, 5, 120}; Integer[] outputArrayWithoutDupElements = removeDuplicatesUsingSet(inputArrayWithDupElements); System.out.println("Original array::::"); for (int element : inputArrayWithDupElements) { System.out.println(element); } System.out.println("After Duplicate Elements removal array:::"); for (int element : outputArrayWithoutDupElements) { System.out.println(element); } } //Method to remove duplicate elements from an array public static Integer[] removeDuplicatesUsingSet(int[] input) { //Adding each array element to a Set Set<Integer> set = new HashSet<Integer>(); for (int element : input) { set.add(element); } Integer[] outputArrayWithoutDupElements = set.toArray(new Integer[set.size()]); return outputArrayWithoutDupElements; } } |
Output of program::
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Original array:::: 5 8 5 50 26 26 100 1001 5 120 After Duplicate Elements removal array::: 50 100 5 8 120 1001 26 |
2. Java program to Remove Duplicate Elements From an Array without using Collection APIs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.kkjavatutorials.client; import java.util.Arrays; /** * @author KK JavaTutorials * Java program to Remove Duplicate Elements From an Array without using Collection APIs */ public class RemoveDuplicateElementsFromArray2 { public static void main(String[] args) { int[] inputArrayWithDupElements = { 5, 8, 5, 50, 26, 26, 100, 1001, 5, 120}; System.out.println("Original array::::"); for (int element : inputArrayWithDupElements) { System.out.println(element); } int[] outputArrayWithoutDupElements = removeDuplicateElementsFromArray(inputArrayWithDupElements); System.out.println("After Duplicate Elements removal array:::"); for (int element : outputArrayWithoutDupElements) { System.out.println(element); } } //Method to remove duplicate elements from an array public static int[] removeDuplicateElementsFromArray(int[] inputArrayWithDupElements) { int i = 1; int j = 0; Arrays.sort(inputArrayWithDupElements); while (i < inputArrayWithDupElements.length) { if (inputArrayWithDupElements[i] == inputArrayWithDupElements[j]) { i++; } else { inputArrayWithDupElements[++j] = inputArrayWithDupElements[i++]; } } /*If array has duplicate elements then after removing duplicate elements This is required to truncate the size of the array otherwise array will retain its original size */ int[] output = Arrays.copyOf(inputArrayWithDupElements, j + 1); return output; } } |
Output of program::
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Original array:::: 5 8 5 50 26 26 100 1001 5 120 After Duplicate Elements removal array::: 5 8 26 50 100 120 1001 |
3. Java program to Remove Duplicate Elements From an Array using Java 8 Stream APIs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.kkjavatutorials.client; import java.util.Arrays; /** * @author KK JavaTutorials * Java program to Remove Duplicate Elements From an Array using Java 8 Stream APIs */ public class RemoveDuplicateElementsFromArray3 { public static void main(String[] args) { int[] inputArrayWithDupElements = { 5, 8, 5, 50, 26, 26, 100, 1001, 5, 120 }; System.out.println("Original array::::"); for (int element : inputArrayWithDupElements) { System.out.println(element); } int outputArrayWithoutDupElements[] = Arrays.stream(inputArrayWithDupElements).distinct().toArray(); System.out.println("After Duplicate Elements removal array:::"); for (int element : outputArrayWithoutDupElements) { System.out.println(element); } } } |
Output of program::
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Original array:::: 5 8 5 50 26 26 100 1001 5 120 After Duplicate Elements removal array::: 5 8 50 26 100 1001 120 |
You May Also Like:
How to swap two numbers with or without temporary variable in java
Java Program to Swap two numbers using Bitwise XOR Operator?
How to reverse a number in Java
How to check Armstrong number java program
Java program to find factorial of a number
Java Program to Calculate the Power of a Number
Check whether a number is prime or not
Java Program to displaying prime numbers
Fibonacci series using iterative and recursive approach java program
Find largest and second largest number in the given Array
Find common elements between two Arrays
Find largest and smallest number in the given Array
Java Program to find duplicate elements in an Array
Count number of words in a string in java
How would you check if a number is even or odd using bit wise operator in Java?
How can you check if the given number is power of 2?
How to check if String is number in java ?
That’s all about this topic Java program to Remove Duplicate Elements From an Array. If you have any doubts or any suggestions to make please drop a comment.