Java Program to find Distinct number in a given Series

Discussion DashboardCategory: JavaJava Program to find Distinct number in a given Series
DHARMEDRA SAHU Staff asked 6 years ago
1 Answers
DHARMEDRA SAHU Staff answered 6 years ago

package com.dharma.corejava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class distinctNumberInSeries {
public static long getDistinctNum(int aMin, int aMax, int bMin, int bMax){
Set set = new HashSet();
long temp = 1;
for(int i=aMin;i<= aMax;i++){
for(int j=bMin;j<=bMax;j++){
temp = 1;
for(int k=0;k<j;k++){
temp *= i;
}
set.add(temp);
}
}
List list = getSortList(set);
Collections.sort(list);
System.out.println("Toatal Distinct Number are :"+list);
return set.size();
}
public static void main(String[] args){

System.out.println("Total distinct Number is:"+getDistinctNum(2,5,2,5));
}

public static List getSortList(Set set){
List list = new ArrayList();
Iterator itr = set.iterator();
while(itr.hasNext()){
list.add(itr.next());
}
return list;
}

}