Count distinct elements in a Vector
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] argv) {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("2");
list.add("c2");
System.out.println(countDistinctElements(list, 0, list.size()));
}
static int countDistinctElements(List source, int position, int count){
List list = source.subList(position, position + count);
Set set = new HashSet();
set.addAll(list);
return set.size();
}
}
Related examples in the same category