This is the code for evaluating factors of given integer array. The problem is I need to find LCM from the list I have received as output. Suggest something for removing duplicate elements from list
Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] ar = {6,12,36};
for (int a:
ar) {
System.out.println(getFactor(a));
}
getFactor(long) returns the list of factors
private static List<Integer> getFactor(long n) {
List<Integer> l = new ArrayList<>();
for (int i=2;n!=1;i++)
{
if(n%i==0)
{
l.add(i);
n=n/i;
i=1;
}
}
return l;
}
/*Input
6,12,36
Output
[2, 3]
[2, 2, 3]
[2, 2, 3, 3]*/
Problem remove the [2,3] duplicates from other lists to get LCM(Least Common Multiple).