java - How many distinct values are inside an array -
say have array :
int [] myarray = 1,2,3,1,4,2,6,1,5,3,6,1;
how can calculate amount of distinct values inside array = 6 ?
int = new treeset<integer>(arrays.aslist(myarray)).size();
that work if myarray
of type integer[]
. in case, simplest way iterate on each element , add set:
set<integer> set = new treeset<integer>(); for(int : myarray) set.add(i); int = set.size();
Comments
Post a Comment