Coding Challenge Series / Technical Interview Series
You have a list of strings. Eliminate duplicates.
Sure, simple enough if the list is small. What if the list has thousands of entries? A simple solution simply
won’t do.
A little bit of everything: software, apps, usability, programming, design and whatever else
Creative — yet I have my doubts that it would be efficient for a large list.
The lack of constraints make this problem really simple.
F#:
let strings = [ "put"; "strings"; "here"; ...... ]
let noDups = strings |> Set.of_list |> Set.to_list
Sorted
@OJ — I don’t know F# — how does it do it’s dirty work? I’m not sure how efficient it would be for a large list? Most functions are optimized for small to medium list. I probably should have emphasized larger than I had …
as my intention was that the function needed to be lightning fast…
Aaron,
The Set functionality in F# is really quick. Behind the scenes it uses a tree to filter out duplicates, so for each item it adds/filters it’d cost O(logN)? Converting back to a list is obviously O(n).
For massive lists it wouldn’t be that fantastic, but you’ll find that the implementation is surprisingly quick as F#’s guts have been optimised quite well.
Cheers.
Using Linq, would this be what you would call too easy?
myListOfStrings.Distinct();