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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -