获取数组中出现频率最高的值 [英] Getting the most frequent value of an array

查看:29
本文介绍了获取数组中出现频率最高的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字数组,我想知道哪个数字在这个数组中出现频率最高.数组有时有 5-6 个整数,有时有 10-12 个,有时甚至更多 - 数组中的整数也可以不同.所以我需要一个可以处理不同长度和数组值的函数.

一个例子:

myArray = [0, 0, 0, 1, 1]

另一个例子:

myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2]

现在我正在寻找一个函数,它给出 0(在第一个例子中)作为 Integer,因为它在这个数组中是 3 次,而数组中的另一个整数 (1) 只是数组中的 2 次.或者对于第二个示例,它将是 4.

这看起来很简单,但我找不到解决方案.在网上找到了一些示例,其中解决方案是使用字典或解决方案很简单 - 但我似乎无法将它与 Swift 3 一起使用...

但是,我没有找到适合我的解决方案.有人知道如何获得整数数组中出现频率最高的整数吗?

解决方案

let myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2]//创建字典以将值映射到计数var counts = [Int: Int]()//使用 forEach 计算值myArray.forEach { counts[$0] = (counts[$0] ?? 0) + 1 }//使用 max(by:) 找到最频繁的值及其计数if let (value, count) = counts.max(by: {$0.1 < $1.1}) {print("\(value) 出现 \(count) 次")}

输出:

<块引用>

4 出现 4 次

<小时>

这里是一个函数:

func mostFrequent(array: [Int]) ->(值:整数,计数:整数)?{var counts = [Int: Int]()array.forEach { counts[$0] = (counts[$0] ?? 0) + 1 }if let (value, count) = counts.max(by: {$0.1 < $1.1}) {返回(值,计数)}//数组为空返回零}if let result = mostFrequent(array: [1, 3, 2, 1, 1, 4, 5]) {print("\(result.value) 出现 \(result.count) 次")}

<块引用>

1 出现 3 次

<小时>

Swift 4 更新:

Swift 4 引入了 reduce(into:_:) 和数组查找的默认值,使您能够在一个有效的行中生成频率.我们不妨让它通用,并让它适用于任何 Hashable 类型:

func mostFrequent(array: [T]) ->(值:T,计数:Int)?{let counts = array.reduce(into: [:]) { $0[$1, default: 0] += 1 }if let (value, count) = counts.max(by: { $0.1 < $1.1 }) {返回(值,计数)}//数组为空返回零}if let result = mostFrequent(array: ["a", "b", "a", "c", "a", "b"]) {print("\(result.value) 出现 \(result.count) 次")}

<块引用><块引用>

a 出现 3 次

I have an Array of numbers and I want to know which number is most frequent in this array. The array sometimes has 5-6 integers, sometimes it has 10-12, sometimes even more - also the integers in the array can be different. So I need a function which can work with different lengths and values of an array.

One example:

myArray = [0, 0, 0, 1, 1]

Another example:

myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2]

Now I am searching for a function which gives out 0 (in the first example) as Integer, as it is 3 times in this array and the other integer in the array (1) is only 2 times in the array. Or for the second example it would be 4.

It seems pretty simple, but I cannot find a solution for this. Found some examples in the web, where the solution is to work with dictionaries or where the solution is simple - but I cannot use it with Swift 3 it seems...

However, I did not find a solution which works for me. Someone has an idea how to get the most frequent integer in an array of integers?

解决方案

let myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2]

// Create dictionary to map value to count   
var counts = [Int: Int]()

// Count the values with using forEach    
myArray.forEach { counts[$0] = (counts[$0] ?? 0) + 1 }

// Find the most frequent value and its count with max(by:)    
if let (value, count) = counts.max(by: {$0.1 < $1.1}) {
    print("\(value) occurs \(count) times")
}

Output:

4 occurs 4 times


Here it is as a function:

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)
    }

    // array was empty
    return nil
}

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

1 occurs 3 times


Update for Swift 4:

Swift 4 introduces reduce(into:_:) and default values for array look ups which enable you to generate the frequencies in one efficient line. And we might as well make it generic and have it work for any type that is Hashable:

func mostFrequent<T: Hashable>(array: [T]) -> (value: T, count: Int)? {

    let counts = array.reduce(into: [:]) { $0[$1, default: 0] += 1 }

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

    // array was empty
    return nil
}

if let result = mostFrequent(array: ["a", "b", "a", "c", "a", "b"]) {
    print("\(result.value) occurs \(result.count) times")
}

a occurs 3 times

这篇关于获取数组中出现频率最高的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆