i need 2 max value array of float number i.e. 1 highest , 1 second highest.is there simple way of getting along indexes or need change array structure this?
just sort array , take required value
var array1 = [2.1, 2.2, 2.5, 3.0, 4.2, 2] var array2 = array1.sort(){ $0 > $1} //one way let firstmax = array2[0] let secondmax = array2[1] //second way let firstmax = array2.removefirst() let secondmax = array2.removefirst()
edit
if want indexes them like
let maxpos = array1.indexof(firstmax) let secmaxpos = array1.indexof(secondmax)
if confused these things follow normal basics follows.
var max = -1.0, maxpos = -1, secmax = -1.0, secmaxpos = -1 (index, value) in array1.enumerate() { if value > max { max = value maxpos = index } else if value > secmax { secmax = value secmaxpos = index } } print("max:\(max)->pos:\(maxpos)::secmax:\(secmax)->secmaxpos:\(secmaxpos)")
Comments
Post a Comment