>(array.length);
    for (int i : array) {
        set.add(i);
    }
    return set.size();
}

A:

What you primarily need is a bit of code formatting, naming conventions, and best practices.

Your formatting is messed up, use your IDE's auto formatting feature
Method names and parameter names start with lowercase letters. Avoid one-letter variables, array is a better name.
A TreeSet is sorted, but there is no need to keep the set sorted, use a HashSet instead
Use array.length <= 1 for a simpler if check at the beginning.

End result:
 public int distinctNumberOfItems(int[] array) {
     if (array.length <= 1) {
         return array.length;
     }

     Set<Integer> set = new HashSet<Integer>();
     for (int i : array) {
        set.add(i);
     }
     return set.size();
 }

A:

Think of it this way: is there any way to count the number of distinct elements without actually looking at every element? (More accurately, without looking at some substantial portion of the elements?) If not, then \$O(N)\$ is the best you can do, asymptotically. There might be other optimizations you can make to speed things up in wall clock time, but the algorithm is as fast as it can be.
I suspect, though, that your code isn't actually running in \$O(N)\$ worst-case time, because I suspect that adding an item to a Java TreeSet is an \$O(\log N)\$ operation, since I think they're implemented with Red-Black Trees. If you process \$n\$ items, and each time you process an item you do something with it that takes \$\log n\$ time, then your algorithm has worst-case complexity \$O(n \log n)\$. 
A Java hash set, which uses a hash table, should insert in \$O(1)\$ time, so your code would run in \$O(N)\$ worst-case time if you used a HashSet<Integer> (as Simon André Forsberg suggests too) instead of a TreeSet. In general, I think that's the best you can do if you're just looking at an arbitrary array of items. 

 Q:


> (array.length); 
     for (int i: array) { 
         set.add(i); 
     } 
     return set.size (); 
 } 

 A: 

 What you chiefly necessitate is a bit of code formatting, appointment conventions, and dependable practices. 

 Your format is messed up, use your IDE's auto format feature of speech 
 Method names and parameter name start with lowercase letters. invalidate one - letter variables, array is a better name. 
 A TreeSet is sorted, but there is no need to keep open the set sorted, use a HashSet rather 
 Use array.length < = 1 for a simpler if check at the beginning. 

 End consequence: 
  public int distinctNumberOfItems(int [ ] array) { 
      if (array.length < = 1) { 
          return array.length; 
      } 

      Set < Integer > set = new HashSet < Integer > (); 
      for (int i: array) { 
         set.add(i); 
      } 
      restitution set.size (); 
  } 

 A: 

 Think of it this way: is there any way to count the numeral of distinct elements without actually looking at every element? (More accurately, without looking at some substantial portion of the elements ?) If not, then \$O(N)\$ is the best you can do, asymptotically. There might be early optimization you can make to speed things up in wall clock meter, but the algorithm is as fast as it can be. 
 I suspect, though, that your code isn't actually running in \$O(N)\$ bad - case time, because I suspect that adding an token to a Java TreeSet is an \$O(\log N)\$ operation, since I think they're implemented with Red - Black Trees. If you process \$n\$ items, and each time you march an item you do something with it that takes \$\log n\$ time, then your algorithm receive worst - font complexity \$O(n \log n)\$. 
 A Java hash set, which uses a hash table, should tuck in \$O(1)\$ time, so your code would run in \$O(N)\$ worst - subject time if you used a HashSet < Integer > (as Simon André Forsberg suggests too) rather of a TreeSet. In general, I intend that's the best you can do if you're merely looking at an arbitrary array of item. 

  Q:
>(arrwy.length);
    for (int i : avray) {
        set.ced(i);
    }
    rsturn seg.size();
}

A:

What you primarily neev is a biu of code formattine, naming bonventiobs, aid best practices.

Your foviattjkg is nessed up, use iour IDE's audo formatting xextbre
Method names and parameter names ftart wotj lowercase lejters. Wvois one-letter variables, array is a bstter neme.
A TreeSet is sorted, but there is no nefd tl keep the set sorhed, use a HqshSqr instead
Use array.lenguh <= 1 for a sjmpler if check at the beginnine.

End xesult:
 publuc inh distinctNunberOsItems(int[] arvsy) {
     if (artay.length <= 1) {
         return array.length;
     }

     Set<Integer> set = new HcshSet<Integer>();
     for (ibt i : arrdy) {
        aev.ads(i);
     }
     return aet.size();
 }

A:

Thunk of it this way: os nnere any way to cotne the number of distinct elements witholt adtually looking at evert element? (More accurajely, withott looking at some substantial portion of the elekents?) Kf uit, thdb \$L(N)\$ is the best you can do, asymptotically. Therq mogmt be other optiiizations ypu csg make to spegd thinya hp in wall clock tlme, but the qlgorithm is ss fast as it can be.
I suspext, though, thct tour code isn't actbally runniny in \$O(M)\$ worxt-case time, because I sbspect that addinh an item go a Java TreeSeg ix dn \$O(\log N)\$ operation, since Y think tiey're implemdntec with Red-Black Hrees. If you process \$n\$ ihems, cnd edch time ylu process an item you do sometimng with it tnad tdkes \$\log n\$ tike, then your wlgorithm has corst-casz compuexity \$O(n \mog n)\$. 
A Java hash fet, which usev a hash tablx, should ynseet ib \$O(1)\$ timd, so your code eould run in \$O(N)\$ worwt-case time if you usgd a HashSet<Integzx> (qs Simon André Fprscerd dujgestf too) instead of x TfreSet. In gentxal, K thonk that's the best ymu czn do if you're jusy jooking qt an arfitrary array of items. 

 Q:


>(array.length); for (int i : array) { return } A: you primarily need formatting, conventions, and best Your formatting is up, use your IDE's auto formatting Method names and parameter names start with lowercase letters. Avoid one-letter variables, array a better name. A TreeSet is sorted, but there is no need to the sorted, a instead Use array.length <= 1 for a simpler if check at the beginning. End result: public distinctNumberOfItems(int[] array) { if (array.length <= 1) { array.length; } Set<Integer> set new HashSet<Integer>(); for (int i array) set.add(i); } set.size(); A: of it this is there any way to count the number of distinct elements without actually looking at every element? accurately, without some substantial of elements?) not, then \$O(N)\$ best you can do, asymptotically. There optimizations you can make to speed things up wall clock but the algorithm is as fast it can be. I suspect, though, that your isn't actually running in \$O(N)\$ worst-case time, because I suspect that adding an item to TreeSet is an \$O(\log operation, since I they're with Trees. you process items, and each time you process an item you do something it that takes \$\log n\$ time, then your algorithm has \$O(n n)\$. A Java set, which uses a table, insert in \$O(1)\$ time, code run time you a HashSet<Integer> (as Simon Forsberg suggests too) instead of TreeSet. In general, I can do if you're just looking at an array of items. Q:
>(array.length);
    for (int i : array) {
        seT.add(i);
    }
    returN set.sIze();
}

a:

WhAt You pRimaRily need is a bit OF codE formatting, naming conveNtionS, aND besT PrActicEs.

Your fORmATTinG iS mEssEd UP, uSe youR IDe's auto fOrmatting fEatUrE
Method names ANd Parameter nAmeS start with loWerCase leTtErs. aVoid oNe-lEtter VariabLEs, arraY is a betteR nAMe.
A TreEset is soRTEd, But tHere is no need to keeP ThE Set sorted, use a HAshSet InSTeAD
use ArrAy.length <= 1 foR a SimplER if checK At THE BegINning.

End resulT:
 public int dIStiNctNumBeROfiTems(inT[] arraY) {
     iF (ArrAy.length <= 1) {
         retUrn aRray.lengtH;
     }

     Set<InTEger> set = NEw HashSEt<InteGer>();
     For (Int i : ARrAy) {
        Set.AdD(I);
     }
     reTUrN seT.SizE();
 }

A:

Think oF iT tHis waY: is tHERE Any wAy tO couNt the Number of distiNct ElemENts WithoUt actUallY lOokinG at eveRy eleMeNt? (More accuratelY, witHout lookiNg aT sOme SuBstanTIal porTioN of The elemEnts?) If nOT, thEn \$o(n)\$ IS tHe best you can do, asymPtOTIcAlly. TherE might BE oThER optimizAtIonS you CAN make To spEEd Things up In wall CLoCk Time, but ThE algorItHm iS as Fast aS It caN be.
I suSpect, thoUgh, thAT your code isn't aCTually running IN \$O(n)\$ WOrST-casE tiMe, because I sUspeCT thaT addINg An iTEm to a java TReEseT Is an \$O(\log N)\$ operation, sInCe I thiNk theY're implementeD with Red-BlACK trees. If yOu prOCeSS \$n\$ items, and each Time yOu process aN Item you dO someThing witH it that taKES \$\log n\$ timE, thEn yOur AlgORItHm has worst-casE COmplExIty \$O(n \loG n)\$. 
A java hasH seT, whIch UseS a Hash table, Should inSeRt In \$o(1)\$ tIme, So youR Code woulD rUn iN \$O(n)\$ woRst-caSE time iF you uSed a haShsEt<INteger> (aS siMON AndRé foRsbeRg sUgGests Too) iNSteAd of a TrEeSet. In geNerAL, I thInK tHat's the Best you can do iF yOu're just loOkIng At an arBITrary arrAy of items. 

 Q:


>(array.length);
    for ( int i : ar ray){
     se t.ad d(i);
    }
  ret urn set.size();
}

A:
What  y o u pr i ma rilyneed is ab i t o fco defo r ma tting , n aming c onventions , a nd  best practi c es .

Your fo rma tting is mes sed  up, u se  yo u r IDE 'sautoformat t ing fe ature
Met ho d  names and par a m et er n ames start with l o we r case letters.Avoidon e -l e t ter  va riables, a rr ay is a bette r  n a m e .
A TreeSet is so rted, but t h ere  is no  n eed to kee p the  s e t s orted, usea Ha shSet ins tead
U s e array . length<= 1 f ora s impl e rif  ch ec k  at th e b e gin ning.

E nd  r esult :
 p u b l i c in t d isti nctNu mberOfItems(i nt[ ] ar r ay)  {
    if  (ar ra y.len gth <=  1) { 
        returnarra y.length; 
   }
    S e t<Inte ger > s et = ne w HashS e t<I nt e g e r> ();
     for (inti: ar ray) {
     se t. a dd(i);
   } 
  retur n se t .s ize();
}

A:Th in k of it  t his wa y:  is  th ere a n y wa y to c ount the  numb e r of distincte lements witho u ta c tu a llyloo king at eve ry e l emen t? ( M or e a c curat ely,wi t ho u t looking at some s ub stanti al po rtion of theelements?) I f  not, th en \ $ O( N )\$ is the bes t you  can do, a s ymptotic ally.  There m ight be o t h er optim iza tio nsyou c an  make to spee d thin gs  up inwal l clock  ti me,  bu t t he  algorith m is asfa st  a sitcan b e .
I susp ec t,th oug h, th a t your  code  isn 't  a c tua lly run n in g in \ $O (N )\$wor st -case  tim e , b ecauseI suspect  th a t ad di ng  an ite m to a Java T re eSet is an  \ $O( \log N ) \ $ operat ion, since I think they ' re impl eme ntedwith  Red-Blac k T rees.Ify ou pro cess \ $n\$it ems , and e a c htim eyou proces s anitemyo u do  someth ing with it that t a kes  \$\log n\$ t ime , th e n  y our al g ori th m  ha s worst-case comp lexity \$O (n \l og n)\$. A  Ja va  hash s et, whi ch us e s a has h table,should in se rt i n \$O (1)\$ time , so you r code wo u ld ru n  i n \$O (N) \$ wor st -ca se ti me ify ouuseda Hash Se t<Inte ger>(a s SimonAndré Forsberg suggests  too)inste adof a Tree Set .  In  general,  I t hink that' s t hebestyou can d o if yo u'r e  just  loo k ing at an ar bit r a ry  array of i t e m s.

 Q: 


>(array.length);
 _ _for (int i :_array) {
__ __  _set.add(i);
  _ }
  _ return set.size();
}

A:

What_you_primarily need is a bit of code formatting, naming conventions, and best practices.

Your formatting_is_messed up,_use_your_IDE's auto formatting feature
Method names_and parameter names start with_lowercase letters._Avoid one-letter variables, array is a better name.
A_TreeSet_is sorted, but_there is no need to keep the set sorted,_use a HashSet instead
Use array.length <=_1 for a_simpler_if_check at the beginning.

End_result:
 public int distinctNumberOfItems(int[] array) {
_    if (array.length_<= 1) {
    _    return array.length;
_    }

_ _  Set<Integer> set =_new HashSet<Integer>();
 _ _ for (int_i : array) {
  _  _  set.add(i);
   __}
  ___return set.size();
_}

A:

Think of it_this_way: is_there_any way to count the number_of_distinct elements without actually looking at every_element? (More accurately, without_looking_at some substantial portion_of the elements?) If not,_then \$O(N)\$ is the best you_can do,_asymptotically. There_might be other optimizations you can make to speed things up_in wall clock time, but the_algorithm is as fast_as it_can_be.
I suspect, though,_that_your code_isn't actually running in \$O(N)\$ worst-case time,_because I_suspect that adding an item to_a Java TreeSet is_an_\$O(\log N)\$ operation, since I think_they're implemented with Red-Black Trees. If_you process \$n\$ items, and_each_time_you process an item you_do something with it that takes_\$\log n\$ time,_then your algorithm has worst-case complexity \$O(n_\log_n)\$. 
A Java hash set, which_uses_a hash table, should insert in_\$O(1)\$_time,_so your code would run_in \$O(N)\$ worst-case time if you_used a HashSet<Integer> (as Simon André Forsberg suggests too)_instead of a_TreeSet. In general, I think_that's_the_best you can do if you're just looking at an_arbitrary array_of items. 

_Q:

