Java Program to remove duplicate element in an Array
Answers
Answered by
3
⚫ Java Program to remove duplicate element in an Array. ⚫
public
class
MyDuplicateElements
{
public
static int[]
removeDuplicates(int[]
input)
{
int j = 0;
int i = 1;
//return if the array length is less than 2 if(input.length < 2)
{
return input;
}
while(i < input.length)
{
if(input[i] == input[j]){ i++;
}
else
{
input[++j] = input[i++];
}
}
int[] output = new int[
j+1]; for(int k=0; k<output.length; k++
){ output[k] =
input[k];
} return output; }
public static void main(String a[]){
int[] input1 = {2,3,6,6,8,9,10,10,10,12,12};
int[] output = removeDuplicates(input1);
for(int i:output){
System.out.print(i+" "); }
}
}
⚫⚫⚫Output ⚫⚫⚫
✅✅✅✅. 2 3 6 8 9 10 12 ✅✅✅✅
public
class
MyDuplicateElements
{
public
static int[]
removeDuplicates(int[]
input)
{
int j = 0;
int i = 1;
//return if the array length is less than 2 if(input.length < 2)
{
return input;
}
while(i < input.length)
{
if(input[i] == input[j]){ i++;
}
else
{
input[++j] = input[i++];
}
}
int[] output = new int[
j+1]; for(int k=0; k<output.length; k++
){ output[k] =
input[k];
} return output; }
public static void main(String a[]){
int[] input1 = {2,3,6,6,8,9,10,10,10,12,12};
int[] output = removeDuplicates(input1);
for(int i:output){
System.out.print(i+" "); }
}
}
⚫⚫⚫Output ⚫⚫⚫
✅✅✅✅. 2 3 6 8 9 10 12 ✅✅✅✅
Answered by
1
Answer:
import java.util.*;
public class Main
{
public static void main(String[] args) {
int[] arr = {5,7,4,1,2,6,9,8,4,5,2,2,5};
LinkedHashSet<Integer> set = new LinkedHashSet<>();
System.out.println("---ARRAY BEFORE REMOVING DUPLICATE ELEMENTS---");
for (int i : arr){
System.out.print(i + " ");
set.add(i);
}
System.out.println("\n---ARRAY AFTER REMOVING DUPLICATE ELEMENTS---");
for(int i : set){
System.out.print(i + " ");
}
}
}
Explanation:
Similar questions
Social Sciences,
7 months ago
English,
7 months ago
Hindi,
1 year ago
English,
1 year ago
Biology,
1 year ago
English,
1 year ago
Business Studies,
1 year ago