根据元素的出现获取数组的子集
Get Subset of array based on the occurrence of elements
我有一个像这样的数组:
var arr = [4,1,5,5,3]
我想根据数组中元素的出现从数组中获取子集。
例如:
Elements with frequency 1 is {4,1,3}
Elements with frequency 2 is {5,5}
我遵循了这个 但无法弄清楚如何做上面的事情。
有什么办法可以做到这一点吗?
你可以使用一个NSCountedSet
来获取arr
中所有元素的计数,然后你可以构建一个Dictionary
,其中的键将是元素和值将是具有键出现次数的元素数组。通过迭代 Set(arr)
而不是简单地 arr
来构建字典,您可以确保重复元素只添加一次到字典中(因此例如对于您的原始示例,不会添加 5频率为 2 的两倍)。
对于打印,您只需要遍历 Dictionary 的键并打印键及其对应的值。我只是对键进行排序,使打印按出现次数的升序排列。
let arr = [4,1,5,5,3,2,3,6,2,7,8,2,7,2,8,8,8,7]
let counts = NSCountedSet(array: arr)
var countDict = [Int:[Int]]()
for element in Set(arr) {
countDict[counts.count(for: element), default: []].append(element)
}
countDict
for freq in countDict.keys.sorted() {
print("Elements with frequency \(freq) are {\(countDict[freq]!)}")
}
输出:
Elements with frequency 1 are {[4, 6, 1]}
Elements with frequency 2 are {[5, 3]}
Elements with frequency 3 are {[7]}
Elements with frequency 4 are {[2, 8]}
Swift 3 版本:
let arr = [4,1,5,5,3,2,3,6,2,7,8,2,7,2,8,8,8,7]
let counts = NSCountedSet(array: arr)
var countDict = [Int:[Int]]()
for element in Set(arr) {
if countDict[counts.count(for: element)] != nil {
countDict[counts.count(for: element)]!.append(element)
} else {
countDict[counts.count(for: element)] = [element]
}
}
for freq in countDict.keys.sorted() {
print("Elements with frequency \(freq) are {\(countDict[freq]!)}")
}
就是这样:
func getSubset(of array: [Int], withFrequency frequency: Int) -> [Int]
{
var counts: [Int: Int] = [:]
for item in array
{
counts[item] = (counts[item] ?? 0) + 1
}
let filtered = counts.filter{ [=10=].value == frequency}
return Array(filtered.keys)
}
这是纯粹的 Swift(没有使用旧的 N
ext S
tep 类)并且使用了您提供的 SO link 中的想法.
counts
字典包含数组中每个整数值(键)的频率(值):[int-value : frequency]
。
您只需要获取元素的出现次数并过滤只出现一次或多次的元素,如answer:
extension Array where Element: Hashable {
// Swift 4 or later
var occurrences: [Element: Int] {
return reduce(into: [:]) { [=10=][, default: 0] += 1 }
}
// // for Swift 3 or earlier
// var occurrences: [Element: Int] {
// var result: [Element: Int] = [:]
// forEach{ result[[=10=]] = (result[[=10=]] ?? 0) + 1}
// return result
// }
func frequencies(where isIncluded: (Int) -> Bool) -> Array {
return filter{ isIncluded(occurrences[[=10=]] ?? 0) }
}
}
游乐场测试:
let arr = [5, 4, 1, 5, 5, 3, 5, 3]
let frequency1 = arr.frequencies {[=11=] == 1} // [4, 1]
let frequency2 = arr.frequencies {[=11=] == 2} // [3, 3]
let frequency3orMore = arr.frequencies {[=11=] >= 3} // [5, 5, 5, 5]
我有一个像这样的数组:
var arr = [4,1,5,5,3]
我想根据数组中元素的出现从数组中获取子集。
例如:
Elements with frequency 1 is {4,1,3}
Elements with frequency 2 is {5,5}
我遵循了这个
有什么办法可以做到这一点吗?
你可以使用一个NSCountedSet
来获取arr
中所有元素的计数,然后你可以构建一个Dictionary
,其中的键将是元素和值将是具有键出现次数的元素数组。通过迭代 Set(arr)
而不是简单地 arr
来构建字典,您可以确保重复元素只添加一次到字典中(因此例如对于您的原始示例,不会添加 5频率为 2 的两倍)。
对于打印,您只需要遍历 Dictionary 的键并打印键及其对应的值。我只是对键进行排序,使打印按出现次数的升序排列。
let arr = [4,1,5,5,3,2,3,6,2,7,8,2,7,2,8,8,8,7]
let counts = NSCountedSet(array: arr)
var countDict = [Int:[Int]]()
for element in Set(arr) {
countDict[counts.count(for: element), default: []].append(element)
}
countDict
for freq in countDict.keys.sorted() {
print("Elements with frequency \(freq) are {\(countDict[freq]!)}")
}
输出:
Elements with frequency 1 are {[4, 6, 1]}
Elements with frequency 2 are {[5, 3]}
Elements with frequency 3 are {[7]}
Elements with frequency 4 are {[2, 8]}
Swift 3 版本:
let arr = [4,1,5,5,3,2,3,6,2,7,8,2,7,2,8,8,8,7]
let counts = NSCountedSet(array: arr)
var countDict = [Int:[Int]]()
for element in Set(arr) {
if countDict[counts.count(for: element)] != nil {
countDict[counts.count(for: element)]!.append(element)
} else {
countDict[counts.count(for: element)] = [element]
}
}
for freq in countDict.keys.sorted() {
print("Elements with frequency \(freq) are {\(countDict[freq]!)}")
}
就是这样:
func getSubset(of array: [Int], withFrequency frequency: Int) -> [Int]
{
var counts: [Int: Int] = [:]
for item in array
{
counts[item] = (counts[item] ?? 0) + 1
}
let filtered = counts.filter{ [=10=].value == frequency}
return Array(filtered.keys)
}
这是纯粹的 Swift(没有使用旧的 N
ext S
tep 类)并且使用了您提供的 SO link 中的想法.
counts
字典包含数组中每个整数值(键)的频率(值):[int-value : frequency]
。
您只需要获取元素的出现次数并过滤只出现一次或多次的元素,如answer:
extension Array where Element: Hashable {
// Swift 4 or later
var occurrences: [Element: Int] {
return reduce(into: [:]) { [=10=][, default: 0] += 1 }
}
// // for Swift 3 or earlier
// var occurrences: [Element: Int] {
// var result: [Element: Int] = [:]
// forEach{ result[[=10=]] = (result[[=10=]] ?? 0) + 1}
// return result
// }
func frequencies(where isIncluded: (Int) -> Bool) -> Array {
return filter{ isIncluded(occurrences[[=10=]] ?? 0) }
}
}
游乐场测试:
let arr = [5, 4, 1, 5, 5, 3, 5, 3]
let frequency1 = arr.frequencies {[=11=] == 1} // [4, 1]
let frequency2 = arr.frequencies {[=11=] == 2} // [3, 3]
let frequency3orMore = arr.frequencies {[=11=] >= 3} // [5, 5, 5, 5]