27 lines
583 B
Plaintext
27 lines
583 B
Plaintext
fn deduplicated<T>(anon array: [T]) throws -> [T] {
|
|
mut existing_items: {T} = {}
|
|
mut result: [T] = []
|
|
for value in array {
|
|
if not existing_items.contains(value) {
|
|
existing_items.add(value)
|
|
result.push(value)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
fn deduplicated_set<T>(anon array: [T]) throws -> {T} {
|
|
mut result: {T} = {}
|
|
for value in array {
|
|
result.add(value)
|
|
}
|
|
return result
|
|
}
|
|
|
|
|
|
fn main() {
|
|
let array = [1, 2, 3, 3, 2, 5, 4]
|
|
println("{}", deduplicated(array))
|
|
println("{}", deduplicated_set(array))
|
|
}
|