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
Post a Comment