ios 我如何在swift中从一个浮点数数组中得到前2个最大值?

kxe2p93d  于 2023-01-06  发布在  iOS
关注(0)|答案(9)|浏览(141)

我需要从一个浮点数数组中获取最大值2,即一个最大值和一个第二大值。有没有简单的方法来获取沿着的索引,或者我需要为此将数组更改为结构?

6jygbczu

6jygbczu1#

只需对数组排序并获取所需的值

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()
    • 编辑**

如果你想要索引,就像这样

let maxPos = array1.indexOf(firstMax)
let secMaxPos = array1.indexOf(secondMax)

如果你对这些东西感到困惑,只要按照下面的正常基础操作就可以了。

var max = -1.0, maxPos = -1, secMax = -1.0, secMaxPos = -1
for (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)")
nkcskrwz

nkcskrwz2#

可以使用enumerate()创建一个包含(index,value)的元组数组,然后按值对该数组进行排序,以查找两个最大值:

let arr: [Float] = [1.2, 3.14, 1.609, 2.718, 0.3]

// Create an array of (index, value) tuples sorted by value
// in decreasing order
let result = arr.enumerate().sort { $0.1 > $1.1 }
print(result)
[(1, 3.1400001), (3, 2.71799994), (2, 1.60899997), (0, 1.20000005), (4, 0.300000012)]
let (topIndex, top) = result[0]
print("top = \(top), index = \(topIndex)")
top = 3.14, index = 1
let (secondIndex, second) = result[1]
print("second = \(second), index = \(secondIndex)")
second = 2.718, index = 3

的一个或多个字符

vwkv1x7d

vwkv1x7d4#

func secondLargest(arr: [Int]){
var max1 = -1;
var max2 = -1;
for item in arr {
    if item > max1 {
        max2 = max1;
        max1 = item
    }else if item > max2 {
        max2 = item
    }
}
}
kpbpu008

kpbpu0085#

尝试在您的发挥gorund
变量项目= [2.01、3.95、1.85、2.65、1.6]
变量排序=项目排序({ $0〉$1 })
打印(已排序)

vd8tlhqk

vd8tlhqk6#

基本思想是循环遍历数组并挑选出最大和第二大的项。
一些可能难以阅读的简短代码:

let myFloatArray : [Float] = ...
let top2 = myFloatArray.enumerated().reduce(((-1, Float.nan), (-1, Float.nan)), combine: { t, v in
    // Check if value is larger than first item in tuple
    if (!(v.1 <= t.0.1)) {
        // Check if value is larger than second item in tuple
        if (!(v.1 <= t.1.1)) {
            // Return with new value as largest
            return (t.1, v)
        } else {
            // Return with new value as next largest
            return (v, t.1)
        }
    }
    // Return old result
    return t
})

或者使用更显式的变量名:

var largestIndex = -1;
var largestValue = Float.nan;
var secondLargestIndex = -1;
var secondLargestValue = Float.nan;
for index in 0..<myFloatArray.count {
    let value = myFloatArray[index];
    if (!(value <= secondLargestValue)) {
        if (!(value <= largestValue)) {
            secondLargestValue = largestValue;
            secondLargestIndex = largestIndex;
            largestValue = value;
            largestIndex = index;
        } else {
            secondLargestValue = value;
            secondLargestIndex = index;
        }
    }
}
yv5phkfx

yv5phkfx7#

变量数组1 = [1,2,3,4,5]
变量数组2 =数组1.sorted(){ $0〉$1}
打印(数组2)
//单向
设第一个最大值=数组2 [0]
设第二个最大值=数组2 [1]
//回答
打印(第一个最大值)
打印(秒最大值)

ego6inou

ego6inou8#

import UIKit

var array = [6,4,3,5,9,7,8,2,10]
var maxValue = 0, temp = 0, maxIndex = 0
for (index, item) in array.enumerated() {
    if (item > temp){
        maxIndex = index
    }
}
array.remove(at: maxIndex)
for (_, item) in array.enumerated() {
   if (item > maxValue){
       maxValue = item
    }
}

print("---------")
print(maxValue)
print("---------")

txu3uszq

txu3uszq9#

var numberArray = [4.4,5.3,3.2,2.1,6.0,1.2,9.6,8.0,9.4]
var secontLargestValue = 0.0
var firstLargestValue = 0.0

for value in numberArray {
    if value > firstLargestValue {
        secontLargestValue = firstLargestValue
        firstLargestValue = value
    } else if (value > secontLargestValue && value != firstLargestValue) 
    {
        secontLargestValue = value
    }
}

print("First largest value - \(firstLargestValue)")
print("Second largest value - \(secontLargestValue)")

相关问题