- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to remove duplicate elements of an array in java?
To detect the duplicate values in an array you need to compare each element of the array to all the remaining elements in case of a match you got your duplicate element.
One solution to do so you need to use two loops (nested) where the inner loop starts with i+1 (where i is the variable of the outer loop) to avoid repetitions.
Apache Commons provides a library named org.apache.commons.lang3 and, following is the maven dependency to add a library to your project.
<dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version> </dependency> </dependencies>
This package provides a class named ArrayUtils using the remove() method of this class you can delete the detected duplicate elements of the given array.
Example
import java.util.Arrays; import java.util.Scanner; import org.apache.commons.lang3.ArrayUtils; public class DeleteDuplicate { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array that is to be created::"); int size = sc.nextInt(); int[] myArray = new int[size]; System.out.println("Enter the elements of the array ::"); for(int i=0; i<size; i++) { myArray[i] = sc.nextInt(); } System.out.println("The array created is ::"+Arrays.toString(myArray)); for(int i=0; i<myArray.length-1; i++) { for (int j=i+1; j<myArray.length; j++) { if(myArray[i] == myArray[j]) { myArray = ArrayUtils.remove(myArray, j); } } } System.out.println("Array after removing elements ::"+Arrays.toString(myArray)); } }
Output
Enter the size of the array that is to be created :: 6 Enter the elements of the array :: 232 232 65 47 89 42 The array created is :: [232, 232, 65, 47, 89, 42] Array after removing elements :: [232, 65, 47, 89, 42]
- Related Articles
- How to remove duplicate elements from an array in JavaScript?
- How to redundantly remove duplicate elements within an array – JavaScript?
- How do I recursively remove consecutive duplicate elements from an array?
- Java program to remove the duplicate element in an array
- Remove duplicate elements in Java with HashSet
- Java Program to Remove duplicate elements from ArrayList
- How to duplicate elements of an array in the same array with JavaScript?
- Remove duplicate element in a Java array.
- Python – Remove Columns of Duplicate Elements
- Python program to print the duplicate elements of an array
- How to remove duplicate property values in array – JavaScript?
- Remove duplicate items from an ArrayList in Java
- Removing duplicate elements from an array in PHP
- C Program to delete the duplicate elements in an array
- How to remove an element from an array in Java

Advertisements