Last active
April 21, 2019 01:08
-
-
Save DaisukeNagata/7a5d2e73999169ea47ae369f3fb5146f to your computer and use it in GitHub Desktop.
配列の重複していない要素数を検索して、該当の要素を配列に入れるロジック
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
//配列の重複していない要素数を検索して、該当の要素を配列に入れるロジック | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let searchWords = ["a","b","c","d","d","c","e","e"] | |
let orderedSet = NSOrderedSet(array: searchWords) | |
let orderArray = (orderedSet.array as? [String])! | |
var indexCheckArray = [String]() | |
for (i, v) in orderArray.enumerated() { | |
if let firstIndex = searchWords.firstIndex(where: {$0 == orderArray[i]}) { | |
//要素番号がわかれば構造体の指定データなどをappendできる。 | |
indexCheckArray.append(v) | |
print("インデックス: \(firstIndex)") | |
} | |
} | |
print(indexCheckArray) | |
} | |
} | |
インデックス: 0 | |
インデックス: 1 | |
インデックス: 2 | |
インデックス: 3 | |
インデックス: 6 | |
["a", "b", "c", "d", "e"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment