The approach I’m referring to is the dual-pointer technique. Where the first pointer is a straightforward iterator and the second pointer goes through only all previous values relative to the first pointer.
That way, less work is done than if, for each node, we compared against every other node. Which would end up being O(n^2).
My question is what is the speed of the dual-pointer method and why?
So if you have
Nelements in the list, doing your de-duping on elementiwill requireicomparisons (there areivalues behind it). So, we can set up the total number of comparisons assum[i = 0 to N] i. This summation evaluates toN(N+1)/2, which is strictly less thanN^2forN > 1.Edit:
To solve the summation, you can approach it like this.
1 + 2 + 3 + 4 + ... + (n-2) + (n-1) + nFrom here, you can match up numbers from opposite sides. This can then become2 + 3 + ... + (n-1) + (n+1)by matching up the1at the start with thenat the end. Do the same with2and(n-1).3 + ... + (n-1+2) + (n+1)simplify to become3 + ... + (n+1) + (n+1)You can repeat this
n/2times, since you are matching up two number each time. This will leave us withn/2occurances of the term(n+1). Multiplying those and simplifying, we get(n+1)(n/2)orn(n+1)/2.See here for more description.
Also, this suggests this summation still has a big-theta of
n^2.