Objective-C: How to find the highest value in an array without sorting? -


im working on assignment in have find highest value in array using loop, , without sorting it. feel have far close being correct not quite there. below example of code have far.

nsarray *unsortedarray = @[ @2, @44, @11, @99, @35 ];   (int = 0; < 4 ; i++) {     id number = [unsortedarray objectatindex:i];     id largestnumber = 0;     if (largestnumber < number) {         largestnumber = number;           nslog(@"%@ largest number", largestnumber );    return 0;     }  } 

now returning first value in area loop stopping, assigning wrong values largestnumber , number? new code, appreciate , feedback , examples.

thanks!

key-value-coding solution, no loop

nsarray *unsortedarray = @[ @2, @44, @11, @99, @35 ]; nsnumber *maxnumber = [unsortedarray valueforkeypath:@"@max.self"]; nslog(@"%@", maxnumber); // 99 

see interesting article mattt thompson: kvc collection operators


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -