删除类型为Double的数组中的最小值和最大值

时间:2019-12-12 11:55:36

标签: arrays swift xcode

我需要删除数组(Double类型)中的MIN和MAX值

我试图获取3的均值。数组总是向其添加值(它是一个计时器) 我需要获取数组中的最后5个值(最近一次),然后删除最小值和最大值,然后将剩余的3个值相加并除以3。

    if oldSessionTimes.count > 5 {
        var mo3WorkOutSuffix = mo3WorkOut.suffix(5)
        let removeMin = mo3WorkOutSuffix.min()
        let removeMax = mo3WorkOutSuffix.max()

        print("Min = \(removeMin!)")
        print("Max = \(removeMax!)")

        print(mo3WorkOutSuffix)

    }

3 个答案:

答案 0 :(得分:3)

var array: [Double] = [1.0, 2.3, 1.5, 1.8]
array = array.sorted().dropFirst().dropLast() // [1.5, 1.8]

答案 1 :(得分:2)

您需要先排序然后删除元素

var array: [Double] = [1.0, 2.3, 1.5, 1.8]
                array.sort()
                array.removeFirst()
                array.removeLast()
                print(array)

答案 2 :(得分:0)

enter code here

in php the array sorting by value ...    

 $num=array(23,32,45,12,15);    
 sort($num);   //this is for sorting by value in ascending order    
 array_shift($num);  //this is for removes first element by the array    
 array_pop($num); //this is for removes last element by the array    
 print_r($num);  //finally print the sorted array and it removes first and last element of array    

 output:[15,23,32];