constsolve=arr=>{constvals=newMap()// tfw no Map::with_capacityfor(leti=0;i<arr.length;i++)vals.set(arr[i],i)constout=newArray(arr.length)for(const[v,i]ofvals)out[i]=vreturnout.filter(x=>typeofx!=="undefined")}
defRemove(duplicate):final=[]foriinrange(len(duplicate)):ifduplicate[i]notinduplicate[i+1:]:final.append(duplicate[i])returnfinalx=input("Enter the integers: ")x=x.split()print(Remove(x))
This is not O(n) because you're iterating over "seen" at every iteration of your for loop, so it's O(n^2) except in the degenerate case where all the elements are duplicates. Also, it isn't specified but unshift itself is also most likely an O(n) operation in most implementations.
But in IntMap documentation, you can see that the actual complexity is O(min(n, W)), where W is size of integer type (32 or 64): hackage.haskell.org/package/contai...
This effectively means that after IntMap contains more than 64 elements, the time complexity is constant O(W) or O(1).
The question remains, does the for in kill the man what if it sorts lexically and not numerically? Should I have just for (const x of sparse) if (typeof x !== "undefined") out[o++] = x?
Maybe
function*isDef(it){for(constxofit)if(typeofx!=="undefined")yieldx}constsolve=arr=>{constvals=newMap()// tfw no Map::with_capacityfor(leti=0;i<arr.length;i++)vals.set(arr[i],i)constsparse=newArray(arr.length)for(const[v,i]ofvals)sparse[i]=vconstout=newArray(vals.length)constsearch=isDef(sparse)for(leti=0;i<vals.length;i++)out[i++]=search.next()returnout}
Here's my python solution O(n). The idea is to iterate over the input and keep track of the index of the last time you saw a given number. If you see a number, replace the old value with None, and do a second pass at the end to remove all the Nones.
The two-phase approach is necessary to keep it O(n). If we remove items as we find them it could end up costing O(n^2).
This solution is in pythonoutput
Hi, you could have use the
setmethod, like this:dev.to/rafaacioly/comment/12l2j
:)
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
In C with O(n2):
Cool!
Thanks, I am new to Programming.
Fixed it
In JavaScript (ES6)
const originalArray = [3,4,4,3,6,3];const distinctArray = [...new Set(originalArray)];
Nope
Simple JS O(n) solution (I think... 2n?) that will remove the left-most duplicates from a list of integers and return the result in the right order.
This is not O(n) because you're iterating over "seen" at every iteration of your for loop, so it's O(n^2) except in the degenerate case where all the elements are duplicates. Also, it isn't specified but unshift itself is also most likely an O(n) operation in most implementations.
I had my doubts, but it seems the JS implementation uses a linear search algorithm.
FWIW, I also had to check if Sets keep insertion ordering since in principle they shouldn't... but in JS they do!
Good to know, I didn't know that was actual documented behavior of the Set.
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):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:
javascript
The question remains, does the
for inkill the man what if it sorts lexically and not numerically? Should I have justfor (const x of sparse) if (typeof x !== "undefined") out[o++] = x?Maybe
ayy lmao
Here's my python solution
O(n). The idea is to iterate over the input and keep track of the index of the last time you saw a given number. If you see a number, replace the old value withNone, and do a second pass at the end to remove all theNones.The two-phase approach is necessary to keep it
O(n). If we remove items as we find them it could end up costingO(n^2).Nim:
Here Goes, My Solution

Python 🐍
is
[3, 4, 6]not
[4, 6, 3]To get the correct result you'd have to do
which is... quite a lot.