In this challenge, you will remove the left-most duplicates from a list of integers and return the result.
// Remove the 3's at indices 0 and 3
// followed by removing a 4 at index 1
solve([3, 4, 4, 3, 6, 3]); // => [4, 6, 3]
Tests:
solve([3,4,4,3,6,3])
solve([1,2,1,2,1,2,3])
solve([1,1,4,5,1,2,1])
solve([1,2,1,2,1,1,3])
Good luck!
This challenge comes from KenKemau on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Oldest comments (30)
This solution is in pythonoutput
Hi, you could have use the
setmethod, like this:dev.to/rafaacioly/comment/12l2j
:)
Nim:
In C with O(n2):
Cool!
Thanks, I am new to Programming.
In JavaScript (ES6)
const originalArray = [3,4,4,3,6,3];const distinctArray = [...new Set(originalArray)];
Nope
Here Goes, My Solution

Javascript in O(n) (more specifically
3n, looping three times on the length of the input, two identicalreducees and onefilter):Epic, but also this relies on the
arraybeing sparse in the first reduce (so, a map really), sincenew Array(Number.MAX_SAFE_INTEGER)is an error.I'd express that as
which is just a funny version of
Python 🐍
Haskell
Test
Wouldn't this have
O(n^2)time complexity? This could be done inO(n), using aIntMap.EDIT:
This is my attempt at writing a similar function but with
O(n)time complexity (O(n*m)whenm <= 64, wheremis amount of elements inIntSet):Hi and thanks for your reply. This looks like a very good solution.
Indeed this algorithm would have an
O(n²)time complexity if thexslist would remain the same. I believe the time complexity isO(n log n)since we are decreasing thexslist each time in the solution I proposed. But I'm bad at time complexity so I wouldn't know.I didn't know about
Data.IntSetI'll look into that. Thanks for sharing.You are forgetting OLog(n) steps used by IntMap internally, it is never O(n),
Perhaps you're mistaking
IntMapforMap?Here, in
Mapdocumentation most lookup and insertion operations are indeedO(log n):hackage.haskell.org/package/contai...
But in
IntMapdocumentation, you can see that the actual complexity isO(min(n, W)), where W is size of integer type (32 or 64):hackage.haskell.org/package/contai...
This effectively means that after
IntMapcontains more than 64 elements, the time complexity is constantO(W)orO(1).Interesting .. I wasn't aware of that.
Here is the simple solution with PHP: