in reply to Identical list members are sometimes counted as different when compared to each other.
The problem with your approach is that you shift all the indexes by one with your splice, but your for loop continually increments $y. You tried to address that problem with if (not $words[$y]) {next}, but that was the wrong fix. The inner loop should visit the words in the reverse order (from the end to the front) to avoid that problem.
Also, your algorithm is very inefficient. It requires the visit of N/2 words (on avergage) for each of the N words in the input list ( O(N2) ). The algorithm I posted earlier is only O(N), so it scales *much* better.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Identical list members are sometimes counted as different when compared to each other.
by di (Acolyte) on Jan 03, 2010 at 14:19 UTC |