swift 数组中的计算模式(多个模式)

xu3bshqb  于 2023-08-02  发布在  Swift
关注(0)|答案(2)|浏览(98)

我在swift中计算模式(数组中最常见的数字)时遇到了问题。例如,在这段代码中

func mostFrequent(array: [Int]) -> (value: Int, count: Int)?
{
    var counts = [Int: Int]()

    array.forEach { counts[$0] = (counts[$0] ?? 0) + 1 }

    if let (value, count) = counts.max(by: {$0.1 < $1.1}) {
        return (value, count)
    }

    return nil
}

if let result = mostFrequent(array: [2,2,2,3,4,4,4,5,6]) {
    print("\(result.value) is repeated \(result.count) times")    
}

字符串
打印:2重复3次
我可以找到第一个最频繁的数字,这是2,重复3次。但是你可以看到,如果有另一个数字也重复了3次,我不能看到它使用这个功能。例如,在我的数字数组中,2重复3次,因此是Mode。但有两种模式,即4,也重复3次。我希望这个功能显示两种模式。有人能帮我指导我怎么做吗?

9avjhtql

9avjhtql1#

你只需要过滤掉等于最大计数的结果,并Map它们的键:

func mostFrequent(array: [Int]) -> (mostFrequent: [Int], count: Int)? {
    var counts: [Int: Int] = [:]
        
    array.forEach { counts[$0] = (counts[$0] ?? 0) + 1 }
    if let count = counts.max(by: {$0.value < $1.value})?.value {
        return (counts.compactMap { $0.value == count ? $0.key : nil }, count)
    }
    return nil
}

个字符
编辑/更新:

extension Sequence where Element: Hashable {
    var frequency: [Element: Int] { reduce(into: [:]) { $0[$1, default: 0] += 1 } }
    var mostFrequent: (mostFrequent: [Element], count: Int)? {
        guard let maxCount = frequency.values.max() else { return nil }
        return (frequency.compactMap { $0.value == maxCount ? $0.key : nil }, maxCount)
    }
}


使用方法:

let array = [2,2,2,3,4,4,4,5,6]
if let mostFrequent = array.mostFrequent {
    print("Most frequent", mostFrequent)
}


这将打印:
最频繁(mostFrequent:[2,4],计数:3)

vu8f3i0k

vu8f3i0k2#

我做了我自己的函数,这是有点jankier,可能有更差的性能,但我只是想张贴这个,如果有人想要一个更容易调整的功能。在我看来,它返回了一组干净的值,并且对每个部分的功能都有清晰的注解。

import Foundation

 func Mode( _ arr: [Double] ) -> ( [Double], Int ) {
     var valueFrequencies = [Double : Int]() // dict. of number and its frequency.
     for i in arr { // for each value in data set
       if !valueFrequencies.keys.contains(i) { // if value has not occurred yet then...
         valueFrequencies[i] = 1 // create a key of that number with a frequency of 1.
       } else { // or if that value has already occurred before then...
           valueFrequencies[i]! += 1 // increase the frequency each time it occurs.
         }
     }
     guard let maxFrequency = valueFrequencies.values.max() else{return ([],0)} // unwrap the mode frequency or return an empty tuple.
     var modes = [Double]() // array for the mode number(s).
     for key in valueFrequencies.keys { // for each key in dict
       if Double(valueFrequencies[key]!) == Double(maxFrequency) { // if the number's frequency is equal to the mode frequency then...
         modes.append(key) // add number to modes array.
       }
     }
     return (modes,maxFrequency) // return array of mode(s) and the mode frequency.
   }

   let dataSet:[Double] = [1,3, 9, 8, 9, 5, 2, 5, 6]
   print(Mode(dataSet)) // Outputs: ([5.0, 9.0], 2)
   print(Mode(dataSet).0) // Outputs: [5.0, 9.0] note: this code does not sort the modes. Values are from a dict. so the order is random.
   print(Mode(dataSet).1) // Outputs: 2

字符串

相关问题