- 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
Remove duplicate element in a Java array.
Following is the required program.
Example
import java.util.Arrays; public class Tester { public static int[] removeDuplicateElements(int arr[]){ int n = arr.length; int[] temp = new int[n]; int j = 0; for (int i=0; i<n-1; i++){ if (arr[i] != arr[i+1]){ temp[j++] = arr[i]; } } temp[j++] = arr[n-1]; return temp; } public static void main (String[] args) { int arr[] = {10,70,30,90,20,20,30,40,70,50}; //sort the array Arrays.sort(arr); int[] result = removeDuplicateElements(arr); //printing array elements for (int i=0; i<result.length; i++) { if(result[i] != 0){ System.out.print(result[i]+" "); } } } }
Output
10 20 30 40 50 70 90
- Related Articles
- Java program to remove the duplicate element in an array
- How to remove duplicate elements of an array in java?
- How to remove a specific element from a JSON Array in Java?
- Remove duplicate elements in Java with HashSet
- Remove/ filter duplicate records from array - JavaScript?
- How to remove an element from an array in Java
- Python program to remove rows with duplicate element in Matrix
- How to remove duplicate property values in array – JavaScript?
- Remove duplicate items from an ArrayList in Java
- How to remove element in a MongoDB array?
- MongoDB aggregation group and remove duplicate array values?
- Remove duplicate items from an array with a custom function in JavaScript
- How to remove duplicate elements from an array in JavaScript?
- Remove an element from a Queue in Java
- Remove an element from a Stack in Java

Advertisements