Remove duplicate element
Problem to remove the duplicate elements in an array.
Example-1:
Input : n = [ 30, 20, 10, 20, 30] Output : [10,20,30] Explain : Remove duplicate element (20,30).
Example-2:
Input : n = [ 50, 20, 10, 20, 40] Output : [50,20,10,40] Explain : Remove duplicate element (20).
Solution
import java.util.HashSet; public class Main { public static void main(String [] args) { int a [] = { 30, 20, 10, 20, 30}; HashSet<Integer> number = new HashSet<Integer>(); for(int i=0;i<a.length;i++) { number.add(a[i]); } System.out.print(number); } }
n = [ 30, 20, 10, 20, 30] a = list(set(n)) print(a)
Output
[20,10,30]