第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何計(jì)算Swift數(shù)組中元素的出現(xiàn)次數(shù)?

如何計(jì)算Swift數(shù)組中元素的出現(xiàn)次數(shù)?

森林海 2019-09-21 15:42:39
我已經(jīng)看到了一些這樣的示例,但是所有這些似乎都依賴于知道要計(jì)算發(fā)生次數(shù)的元素。我的數(shù)組是動(dòng)態(tài)生成的,所以我無法知道要計(jì)算哪個(gè)元素的出現(xiàn)(我想計(jì)算所有元素的出現(xiàn))。有人可以建議嗎?提前致謝編輯:也許我應(yīng)該更清楚一點(diǎn),數(shù)組將包含多個(gè)不同的字符串(例如 ["FOO", "FOO", "BAR", "FOOBAR"]我如何在不知道它們是什么的情況下計(jì)算foo,bar和foobar的出現(xiàn)?
查看完整描述

3 回答

?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊

使用Swift 5時(shí),您可以根據(jù)需要選擇以下7個(gè)Playground示例代碼之一來計(jì)算數(shù)組中可哈希項(xiàng)的出現(xiàn)次數(shù)。


#1。使用Array的reduce(into:_:)和Dictionary的subscript(_:default:)標(biāo)

let array = [4, 23, 97, 97, 97, 23]

let dictionary = array.reduce(into: [:]) { counts, number in

    counts[number, default: 0] += 1

}

print(dictionary) // [4: 1, 23: 2, 97: 3]

#2。使用repeatElement(_:count:)函數(shù),zip(_:_:)函數(shù)和Dictionary的init(_:uniquingKeysWith:)初始值設(shè)定項(xiàng)

let array = [4, 23, 97, 97, 97, 23]


let repeated = repeatElement(1, count: array.count)

//let repeated = Array(repeating: 1, count: array.count) // also works


let zipSequence = zip(array, repeated)


let dictionary = Dictionary(zipSequence, uniquingKeysWith: { (current, new) in

    return current + new

})

//let dictionary = Dictionary(zipSequence, uniquingKeysWith: +) // also works


print(dictionary) // prints [4: 1, 23: 2, 97: 3]

#3。使用Dictionary的init(grouping:by:)初始值設(shè)定項(xiàng)和mapValues(_:)方法

let array = [4, 23, 97, 97, 97, 23]


let dictionary = Dictionary(grouping: array, by: { $0 })


let newDictionary = dictionary.mapValues { (value: [Int]) in

    return value.count

}


print(newDictionary) // prints: [97: 3, 23: 2, 4: 1]

#4。使用Dictionary的init(grouping:by:)初始值設(shè)定項(xiàng)和map(_:)方法

let array = [4, 23, 97, 97, 97, 23]


let dictionary = Dictionary(grouping: array, by: { $0 })


let newArray = dictionary.map { (key: Int, value: [Int]) in

    return (key, value.count)

}


print(newArray) // prints: [(4, 1), (23, 2), (97, 3)]

#5。使用for循環(huán)和Dictionary的subscript(_:)下標(biāo)

extension Array where Element: Hashable {


    func countForElements() -> [Element: Int] {

        var counts = [Element: Int]()

        for element in self {

            counts[element] = (counts[element] ?? 0) + 1

        }

        return counts

    }


}


let array = [4, 23, 97, 97, 97, 23]

print(array.countForElements()) // prints [4: 1, 23: 2, 97: 3]

#6。使用NSCountedSet和NSEnumerator的map(_:)方法(需要Foundation)

import Foundation


extension Array where Element: Hashable {


    func countForElements() -> [(Element, Int)] {

        let countedSet = NSCountedSet(array: self)

        let res = countedSet.objectEnumerator().map { (object: Any) -> (Element, Int) in

            return (object as! Element, countedSet.count(for: object))

        }

        return res

    }


}


let array = [4, 23, 97, 97, 97, 23]

print(array.countForElements()) // prints [(97, 3), (4, 1), (23, 2)]

#7。使用NSCountedSet和AnyIterator(需要Foundation)

import Foundation


extension Array where Element: Hashable {


    func counForElements() -> Array<(Element, Int)> {

        let countedSet = NSCountedSet(array: self)

        var countedSetIterator = countedSet.objectEnumerator().makeIterator()

        let anyIterator = AnyIterator<(Element, Int)> {

            guard let element = countedSetIterator.next() as? Element else { return nil }

            return (element, countedSet.count(for: element))

        }

        return Array<(Element, Int)>(anyIterator)

    }


}


let array = [4, 23, 97, 97, 97, 23]

print(array.counForElements()) // [(97, 3), (4, 1), (23, 2)]


查看完整回答
反對(duì) 回復(fù) 2019-09-21
  • 3 回答
  • 0 關(guān)注
  • 1136 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)