Remove duplicate elements: Difference between revisions

Jakt
(Jakt)
Line 2,918:
0 1 2
0 2 4</syntaxhighlight>
 
=={{header|Jakt}}==
The array version preserves order based on first occurrence.
 
<syntaxhighlight lang="jakt">
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))
}
</syntaxhighlight>
 
=={{header|Java}}==
89

edits