Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数. 一个例子: myArray = [0, 0, 0, 1, 1] 另一个例子: myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2] 现在我正在寻找一个函数,它给出0(在第一个 我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数.

一个例子:

myArray = [0,1,1]

另一个例子:

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

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

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

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

let myArray = [4,2]// Create dictionary to map value to count   var counts = [Int: Int]()// Count the values with using forEach    myArray.forEach { counts[
func mostFrequent(array: [Int]) -> (value: Int,count: Int)? {    var counts = [Int: Int]()    array.forEach { counts[
func mostFrequent<T: Hashable>(array: [T]) -> (value: T,count: Int)? {    let counts = array.reduce(into: [:]) { [+++][,default: 0] += 1 }    if let (value,count) = counts.max(by: { [+++].1 < .1 }) {        return (value,count)    }    // array was empty    return nil}if let result = mostFrequent(array: ["a","b","a","c","b"]) {    print("\(result.value) occurs \(result.count) times")}
] = (counts[[+++]] ?? 0) + 1 } if let (value,count) = counts.max(by: {[+++].1 < .1}) { return (value,count) } // array was empty return nil}if let result = mostFrequent(array: [1,2,5]) { print("\(result.value) occurs \(result.count) times") }
] = (counts[[+++]] ?? 0) + 1 }// Find the most frequent value and its count with max(by:) if let (value,count) = counts.max(by: {[+++].1 < .1}) { print("\(value) occurs \(count) times")}

输出:

06001

这是一个功能:

[+++]

06003

Swift 4的更新:

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

[+++]

06005

总结

以上是内存溢出为你收集整理的数组 – 获取数组的最常见值全部内容,希望文章能够帮你解决数组 – 获取数组的最常见值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 6, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数. 一个例子: myArray = [0, 0, 0, 1, 1] 另一个例子: myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2] 现在我正在寻找一个函数,它给出0(在第一个 我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数.

一个例子:

myArray = [0,1,1]

另一个例子:

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

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

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

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

let myArray = [4,2]// Create dictionary to map value to count   var counts = [Int: Int]()// Count the values with using forEach    myArray.forEach { counts[
func mostFrequent(array: [Int]) -> (value: Int,count: Int)? {    var counts = [Int: Int]()    array.forEach { counts[
func mostFrequent<T: Hashable>(array: [T]) -> (value: T,count: Int)? {    let counts = array.reduce(into: [:]) { [,default: 0] += 1 }    if let (value,count) = counts.max(by: { [+++].1 < .1 }) {        return (value,count)    }    // array was empty    return nil}if let result = mostFrequent(array: ["a","b","a","c","b"]) {    print("\(result.value) occurs \(result.count) times")}
] = (counts[[+++]] ?? 0) + 1 } if let (value,count) = counts.max(by: {[+++].1 < .1}) { return (value,count) } // array was empty return nil}if let result = mostFrequent(array: [1,2,5]) { print("\(result.value) occurs \(result.count) times") }
] = (counts[[+++]] ?? 0) + 1 }// Find the most frequent value and its count with max(by:) if let (value,count) = counts.max(by: {[+++].1 < .1}) { print("\(value) occurs \(count) times")}

输出:

06001

这是一个功能:

[+++]

06003

Swift 4的更新:

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

[+++]

06005

总结

以上是内存溢出为你收集整理的数组 – 获取数组的最常见值全部内容,希望文章能够帮你解决数组 – 获取数组的最常见值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 7, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数. 一个例子: myArray = [0, 0, 0, 1, 1] 另一个例子: myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2] 现在我正在寻找一个函数,它给出0(在第一个 我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数.

一个例子:

myArray = [0,1,1]

另一个例子:

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

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

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

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

let myArray = [4,2]// Create dictionary to map value to count   var counts = [Int: Int]()// Count the values with using forEach    myArray.forEach { counts[
func mostFrequent(array: [Int]) -> (value: Int,count: Int)? {    var counts = [Int: Int]()    array.forEach { counts[
func mostFrequent<T: Hashable>(array: [T]) -> (value: T,count: Int)? {    let counts = array.reduce(into: [:]) { [,default: 0] += 1 }    if let (value,count) = counts.max(by: { .1 < .1 }) {        return (value,count)    }    // array was empty    return nil}if let result = mostFrequent(array: ["a","b","a","c","b"]) {    print("\(result.value) occurs \(result.count) times")}
] = (counts[[+++]] ?? 0) + 1 } if let (value,count) = counts.max(by: {[+++].1 < .1}) { return (value,count) } // array was empty return nil}if let result = mostFrequent(array: [1,2,5]) { print("\(result.value) occurs \(result.count) times") }
] = (counts[[+++]] ?? 0) + 1 }// Find the most frequent value and its count with max(by:) if let (value,count) = counts.max(by: {[+++].1 < .1}) { print("\(value) occurs \(count) times")}

输出:

06001

这是一个功能:

[+++]

06003

Swift 4的更新:

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

[+++]

06005

总结

以上是内存溢出为你收集整理的数组 – 获取数组的最常见值全部内容,希望文章能够帮你解决数组 – 获取数组的最常见值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 8, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数. 一个例子: myArray = [0, 0, 0, 1, 1] 另一个例子: myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2] 现在我正在寻找一个函数,它给出0(在第一个 我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数.

一个例子:

myArray = [0,1,1]

另一个例子:

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

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

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

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

let myArray = [4,2]// Create dictionary to map value to count   var counts = [Int: Int]()// Count the values with using forEach    myArray.forEach { counts[
func mostFrequent(array: [Int]) -> (value: Int,count: Int)? {    var counts = [Int: Int]()    array.forEach { counts[
func mostFrequent<T: Hashable>(array: [T]) -> (value: T,count: Int)? {    let counts = array.reduce(into: [:]) { [,default: 0] += 1 }    if let (value,count) = counts.max(by: { .1 < .1 }) {        return (value,count)    }    // array was empty    return nil}if let result = mostFrequent(array: ["a","b","a","c","b"]) {    print("\(result.value) occurs \(result.count) times")}
] = (counts[] ?? 0) + 1 } if let (value,count) = counts.max(by: {[+++].1 < .1}) { return (value,count) } // array was empty return nil}if let result = mostFrequent(array: [1,2,5]) { print("\(result.value) occurs \(result.count) times") }
] = (counts[[+++]] ?? 0) + 1 }// Find the most frequent value and its count with max(by:) if let (value,count) = counts.max(by: {[+++].1 < .1}) { print("\(value) occurs \(count) times")}

输出:

06001

这是一个功能:

[+++]

06003

Swift 4的更新:

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

[+++]

06005

总结

以上是内存溢出为你收集整理的数组 – 获取数组的最常见值全部内容,希望文章能够帮你解决数组 – 获取数组的最常见值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 9, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数. 一个例子: myArray = [0, 0, 0, 1, 1] 另一个例子: myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2] 现在我正在寻找一个函数,它给出0(在第一个 我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数.

一个例子:

myArray = [0,1,1]

另一个例子:

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

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

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

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

let myArray = [4,2]// Create dictionary to map value to count   var counts = [Int: Int]()// Count the values with using forEach    myArray.forEach { counts[
func mostFrequent(array: [Int]) -> (value: Int,count: Int)? {    var counts = [Int: Int]()    array.forEach { counts[
func mostFrequent<T: Hashable>(array: [T]) -> (value: T,count: Int)? {    let counts = array.reduce(into: [:]) { [,default: 0] += 1 }    if let (value,count) = counts.max(by: { .1 < .1 }) {        return (value,count)    }    // array was empty    return nil}if let result = mostFrequent(array: ["a","b","a","c","b"]) {    print("\(result.value) occurs \(result.count) times")}
] = (counts[] ?? 0) + 1 } if let (value,count) = counts.max(by: {.1 < .1}) { return (value,count) } // array was empty return nil}if let result = mostFrequent(array: [1,2,5]) { print("\(result.value) occurs \(result.count) times") }
] = (counts[[+++]] ?? 0) + 1 }// Find the most frequent value and its count with max(by:) if let (value,count) = counts.max(by: {[+++].1 < .1}) { print("\(value) occurs \(count) times")}

输出:

06001

这是一个功能:

[+++]

06003

Swift 4的更新:

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

[+++]

06005

总结

以上是内存溢出为你收集整理的数组 – 获取数组的最常见值全部内容,希望文章能够帮你解决数组 – 获取数组的最常见值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 10, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数. 一个例子: myArray = [0, 0, 0, 1, 1] 另一个例子: myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2] 现在我正在寻找一个函数,它给出0(在第一个 我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数.

一个例子:

myArray = [0,1,1]

另一个例子:

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

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

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

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

let myArray = [4,2]// Create dictionary to map value to count   var counts = [Int: Int]()// Count the values with using forEach    myArray.forEach { counts[
func mostFrequent(array: [Int]) -> (value: Int,count: Int)? {    var counts = [Int: Int]()    array.forEach { counts[
func mostFrequent<T: Hashable>(array: [T]) -> (value: T,count: Int)? {    let counts = array.reduce(into: [:]) { [,default: 0] += 1 }    if let (value,count) = counts.max(by: { .1 < .1 }) {        return (value,count)    }    // array was empty    return nil}if let result = mostFrequent(array: ["a","b","a","c","b"]) {    print("\(result.value) occurs \(result.count) times")}
] = (counts[] ?? 0) + 1 } if let (value,count) = counts.max(by: {.1 < .1}) { return (value,count) } // array was empty return nil}if let result = mostFrequent(array: [1,2,5]) { print("\(result.value) occurs \(result.count) times") }
] = (counts[] ?? 0) + 1 }// Find the most frequent value and its count with max(by:) if let (value,count) = counts.max(by: {[+++].1 < .1}) { print("\(value) occurs \(count) times")}

输出:

06001

这是一个功能:

[+++]

06003

Swift 4的更新:

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

[+++]

06005

总结

以上是内存溢出为你收集整理的数组 – 获取数组的最常见值全部内容,希望文章能够帮你解决数组 – 获取数组的最常见值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 11, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数. 一个例子: myArray = [0, 0, 0, 1, 1] 另一个例子: myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2] 现在我正在寻找一个函数,它给出0(在第一个 我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数.

一个例子:

myArray = [0,1,1]

另一个例子:

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

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

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

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

let myArray = [4,2]// Create dictionary to map value to count   var counts = [Int: Int]()// Count the values with using forEach    myArray.forEach { counts[
func mostFrequent(array: [Int]) -> (value: Int,count: Int)? {    var counts = [Int: Int]()    array.forEach { counts[
func mostFrequent<T: Hashable>(array: [T]) -> (value: T,count: Int)? {    let counts = array.reduce(into: [:]) { [,default: 0] += 1 }    if let (value,count) = counts.max(by: { .1 < .1 }) {        return (value,count)    }    // array was empty    return nil}if let result = mostFrequent(array: ["a","b","a","c","b"]) {    print("\(result.value) occurs \(result.count) times")}
] = (counts[] ?? 0) + 1 } if let (value,count) = counts.max(by: {.1 < .1}) { return (value,count) } // array was empty return nil}if let result = mostFrequent(array: [1,2,5]) { print("\(result.value) occurs \(result.count) times") }
] = (counts[] ?? 0) + 1 }// Find the most frequent value and its count with max(by:) if let (value,count) = counts.max(by: {.1 < .1}) { print("\(value) occurs \(count) times")}

输出:

06001

这是一个功能:

[+++]

06003

Swift 4的更新:

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

[+++]

06005

总结

以上是内存溢出为你收集整理的数组 – 获取数组的最常见值全部内容,希望文章能够帮你解决数组 – 获取数组的最常见值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 12, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数. 一个例子: myArray = [0, 0, 0, 1, 1] 另一个例子: myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2] 现在我正在寻找一个函数,它给出0(在第一个 我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数.

一个例子:

myArray = [0,1,1]

另一个例子:

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

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

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

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

let myArray = [4,2]// Create dictionary to map value to count   var counts = [Int: Int]()// Count the values with using forEach    myArray.forEach { counts[
func mostFrequent(array: [Int]) -> (value: Int,count: Int)? {    var counts = [Int: Int]()    array.forEach { counts[
func mostFrequent<T: Hashable>(array: [T]) -> (value: T,count: Int)? {    let counts = array.reduce(into: [:]) { [,default: 0] += 1 }    if let (value,count) = counts.max(by: { .1 < .1 }) {        return (value,count)    }    // array was empty    return nil}if let result = mostFrequent(array: ["a","b","a","c","b"]) {    print("\(result.value) occurs \(result.count) times")}
] = (counts[] ?? 0) + 1 } if let (value,count) = counts.max(by: {.1 < .1}) { return (value,count) } // array was empty return nil}if let result = mostFrequent(array: [1,2,5]) { print("\(result.value) occurs \(result.count) times") }
] = (counts[] ?? 0) + 1 }// Find the most frequent value and its count with max(by:) if let (value,count) = counts.max(by: {.1 < .1}) { print("\(value) occurs \(count) times")}

输出:

06001

这是一个功能:

06003

Swift 4的更新:

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

[+++]

06005

总结

以上是内存溢出为你收集整理的数组 – 获取数组的最常见值全部内容,希望文章能够帮你解决数组 – 获取数组的最常见值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
数组 – 获取数组的最常见值_app_内存溢出

数组 – 获取数组的最常见值

数组 – 获取数组的最常见值,第1张

概述我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数. 一个例子: myArray = [0, 0, 0, 1, 1] 另一个例子: myArray = [4, 4, 4, 3, 3, 3, 4, 6, 6, 5, 5, 2] 现在我正在寻找一个函数,它给出0(在第一个 我有一个数字数组,我想知道这个数组中最常见的数字.数组有时有5-6个整数,有时它有10-12个,有时甚至更多 – 数组中的整数也可能不同.所以我需要一个可以使用不同长度和数组值的函数.

一个例子:

myArray = [0,1,1]

另一个例子:

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

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

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

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

let myArray = [4,2]// Create dictionary to map value to count   var counts = [Int: Int]()// Count the values with using forEach    myArray.forEach { counts[
func mostFrequent(array: [Int]) -> (value: Int,count: Int)? {    var counts = [Int: Int]()    array.forEach { counts[
func mostFrequent<T: Hashable>(array: [T]) -> (value: T,count: Int)? {    let counts = array.reduce(into: [:]) { [,default: 0] += 1 }    if let (value,count) = counts.max(by: { .1 < .1 }) {        return (value,count)    }    // array was empty    return nil}if let result = mostFrequent(array: ["a","b","a","c","b"]) {    print("\(result.value) occurs \(result.count) times")}
] = (counts[] ?? 0) + 1 } if let (value,count) = counts.max(by: {.1 < .1}) { return (value,count) } // array was empty return nil}if let result = mostFrequent(array: [1,2,5]) { print("\(result.value) occurs \(result.count) times") }
] = (counts[] ?? 0) + 1 }// Find the most frequent value and its count with max(by:) if let (value,count) = counts.max(by: {.1 < .1}) { print("\(value) occurs \(count) times")}

输出:

06001

这是一个功能:

06003

Swift 4的更新:

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

06005

总结

以上是内存溢出为你收集整理的数组 – 获取数组的最常见值全部内容,希望文章能够帮你解决数组 – 获取数组的最常见值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1048386.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存