2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift】dictionaryでvalueの最大値だけを取得する

Posted at

日本語のサイトで,ぱっと出てこなかったので自分用に記録.
Optional型がまだいまいち理解できておらずForced unwrappingをガンガン使ってしまうが、よくないんだろうな...

dictionaryでvalue最大値を取得

dictionary.swift
let dictionary: [String: Int] = ["1": 55, "2": 104, "3": 9]
let greatestDic = dictionary.max { a, b in a.value < b.value }

print(greatestDic) // -> "Optional(("2": 104))"

let maxValue = greatestDic!.value
print(maxValue) // -> 104

配列・辞書の使い方が難しい

入力値が増えてくると多次元配列をどう扱っていいか、辞書を使ったほうがいいのか等々うまくコントロールできなくなってきています。
こういう問題は競技プログラミング的な勉強をしたほうがいいのだろうか。

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?