Java Program for Removing Duplicates elements from an array
Removing Duplicates elements from an array in Java
Here, in this page we will discuss the program for removing duplicates elements from an array in java programming language. We are given with an array and need to print the array that only consist of distinct elements.Method Discussed :
- Method 1 : Using Set Data Structure
- Method 2 : Without Using Extra Space.
Let’s discuss both methods one by one in brief.
Method 1 :
- Declare a set data structure.
- Insert the elements of array in set.
- Print the set
Time and Space Complexity :
- Time Complexity : O(n)
- Space Complexity : O(n)
Set in Java
The set interface is present in java.util package and extends the Collection interface is an unordered collection of objects in which duplicate values cannot be stored.It is an interface that implements the mathematical set.
This interface contains the methods inherited from the Collection interface and adds a feature that restricts the insertion of the duplicate elements.
Method 1 : Code in Java
Run
import java.util.*; class Main { public static void main (String[] args) { int arr[] = {10, 20, 20, 30, 40, 40, 40, 50, 50}; int n = arr.length; Set hash_Set = new HashSet(); for (int i=0; i<n; i++) hash_Set.add(arr[i]); System.out.print(hash_Set); } }
Output :
[50, 20, 40, 10, 30]
Method 2 :
In this method we will not use extra space.
- Create a function say duplicate that will return the index value.
- if(n==0) or (n==1) then return n.
- Otherwise take a variable say j set j =0;
- Run a loop from index 0 to n-1
- If(arr[i]!=arr[i+1]), Then set arr[j++] = arr[i]
- Finally, return the value of j
Time and Space Complexity :
- Time Complexity : O(n)
- Space Complexity : O(1)
Method 2 : Code in Java
Run
class Main { static int removeDuplicates(int arr[], int n) { if (n==0 || n==1) return n; int j = 0; for (int i=0; i<n-1; i++) if (arr[i] != arr[i+1]) arr[j++] = arr[i]; arr[j++] = arr[n-1]; return j; } public static void main (String[] args) { int arr[] = {10, 20, 20, 30, 40, 40, 40, 50, 50}; int n = arr.length; n = removeDuplicates(arr, n); for (int i=0; i<n; i++) System.out.print(arr[i]+" "); } }
Output :
10 20 30 40 50
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
import java.util.*;
public class Exam1 {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“enter size of 1st array”);
int n1=sc.nextInt();
System.out.println(“enter size of 2nd array”);
int n2=sc.nextInt();
int n3=n1+n2;
int arr1[]=new int[n1];
int arr2[]=new int[n2];
int arr3[]=new int[n3];
int k=0;
System.out.println(“enter elements of 1st array”);
for(int i=0;i<n1;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("enter elements of 2nd array");
for(int i=0;i<n2;i++)
{
arr2[i]=sc.nextInt();
}
for(int i=0;i<n1;i++) {
arr3[k++] = arr1[i];
}
for(int i=0;i<n2;i++)
{
arr3[k++]=arr2[i];
}
System.out.println("after merging resulting array is");
for(int i=0;i<n3;i++)
{
System.out.println(arr3[i]+" ");
}
for(int i=0;i<n3;i++)
{
for(int j=i+1;j<n3;j++)
{
if(arr3[i]==arr3[j])
{
arr3[i]=-1;
}
}
}
System.out.println("after removing duplicate element array is");
for(int i=0;i<n3;i++)
{
if(arr3[i]!=-1)
{
System.out.print(arr3[i]+" ");
}
}
}
}
Hey there, Kindly join our discord channel for all Technical queries. Our mentors are right there to help you with it.
import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println(“enter size and elements”);
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int arr[] = new int [n];
for(int i =0; i<n; i++){
arr[i]= sc.nextInt();
}
int temp [] = new int [n];
int j =0;
Arrays.sort(arr);
for(int i =0; i<n-1; i++){
if(arr[i]!=arr[i+1])
temp[j++]=arr[i];
}
temp[j++]=arr[n-1];
for(int k =0; k<j; k++){
System.out.print(temp[k] + " ");
}
}
}
Hey there, Thanks for commenting, kindly join our Discord server, our mentors will guide you further precisely will all your queries.🙌
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int a[]={10,10,20,30,20};
int n=a.length;
HashSet H=new HashSet();
for(int i=0;i<n;i++){
H.add(a[i]);
}
System.out.println(H);
}
}
Time Complexity: O(n)
Space Complexity: O(n)
Hey, Join our Discord channel for any technical queries
//Removing Duplicate Elements in an Array
class Lakhan
{
public static void main(String ar[])
{
int x[]={10, 20, 20, 30, 40, 40, 40, 50, 50};
int n=x.length;
int f=0;
for(int i=0;i<n;i++)
{
f=0;
for(int j=i+1;j<n;j++)
{
if(x[i] == x[j])
{
f=1;
}
}
if(f==1)
continue;
System.out.print(" "+x[i]);
}
}
}
// Using Ste
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=scn.nextInt();
}
Set set=new TreeSet();
for(int i=0;i<arr.length;i++){
set.add(arr[i]);
}
System.out.print(set);
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=scn.nextInt();
}
for(int i=0;i<arr.length;i++){
if(arr[i]!=-1){
for(int j=i+1;j<arr.length;j++){
if(arr[i]==arr[j]){
arr[j]=-1;
}
}
}
}
for(int temp:arr){
if(temp!=-1){
System.out.print(temp+" ");
}
}
}
}
public class RemoveDuplicateEle {
public static void duplicateElement(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j] && arr[i] != 0) {
arr[j] = 0;
}
}
if(arr[i]!=0){
System.out.print(arr[i]+" ");
}
}
System.out.println();
}
public static void main(String[] args) {
int arr[] = { 10, 30, 40, 20, 10, 30, 65, 74, 65, 20, 50, 10 };
int n = arr.length;
duplicateElement(arr, n);
}
}