Last active
September 29, 2021 14:50
-
-
Save treastrain/42163a4854f6e98a3e92d902db46faa3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// main.swift | |
// | |
// Created by treastrain / Tanaka Ryoga on 2021/09/12. | |
// | |
// These codes are licensed under CC0. | |
// https://creativecommons.org/publicdomain/zero/1.0/deed | |
// | |
import Foundation | |
extension Array where Element: Collection, Element.Element: Equatable { | |
func orderdSetWithFlag() -> [Element.Element?] { | |
var elements = [Element.Element?]() | |
self.forEach { collection in | |
collection.forEach { element in | |
if !elements.contains(element) { | |
if collection.first == element { | |
elements.append(nil) | |
} | |
elements.append(element) | |
} | |
} | |
} | |
elements.removeFirst() | |
return elements | |
} | |
} | |
let arrays1: [[Int]] = [ | |
[1, 2, 3, 4], | |
[3, 4, 5, 6], | |
[8, 9, 10, 11], | |
[8, 9, 10, 11], | |
[11, 12, 13, 14], | |
[15, 16, 17, 18], | |
] | |
arrays1.orderdSetWithFlag().forEach { | |
print($0 ?? "nil", terminator: "") | |
print(", ", terminator: "") | |
} // 1, 2, 3, 4, 5, 6, nil, 8, 9, 10, 11, 12, 13, 14, nil, 15, 16, 17, 18, | |
let arrays2: [[String]] = [ | |
["a", "b", "c", "d"], | |
["b", "c", "d", "e"], | |
["e", "f", "g", "h"], | |
["i", "j", "k", "l"], | |
["n", "o", "p", "q"], | |
] | |
arrays2.orderdSetWithFlag().forEach { | |
print($0 ?? "nil", terminator: "") | |
print(", ", terminator: "") | |
} // a, b, c, d, e, f, g, h, nil, i, j, k, l, nil, n, o, p, q, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👏