获取数组的最频繁值 [英] Getting the most frequent value of an array

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

问题描述

我有一个数字数组,我想知道该数组中哪个数字最常见.数组有时包含5-6个整数,有时包含10-12个,有时甚至更多-数组中的整数也可以不同.因此,我需要一个可以与数组的不同长度和值一起使用的函数.

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.

一个例子:

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.

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.

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

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

输出:

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


Swift 4更新:

Swift 4引入了reduce(into:_:)和数组查找的默认值,使您可以在一条有效的行中生成频率.我们也可以使其通用并使其适用于Hashable:

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天全站免登陆