Remove duplicate values from a List in F#
Once in a while I give a look at the web server log to find out how people landed in this blog and yesterday there was someone that entered the following search string into Google:
Given a random List of integers, write a function that removes all the duplicate values
This site appears as the first result, even if there is no solution for that problem, but I want to help you anyway, unknown visitor!
In fact, we can make use of the Set data type to easily solve the problem.
According to the definition, a Set represents a collection of elements without duplicates.
So, if we create a Set from the given list, all duplicate values will be automatically filtered.
This can be done in a single line, I'll call the function nub because this is the name of the corresponding Haskell one:
let nub xs = xs |> Set.of_list |> Set.to_list
As I said, to return a List without duplicate values we just need to create a Set from it and then create a List from the new Set.
Do you think it is fair to act this way or should I say that I'm cheating?
Twitter Updates
- @Sabrina_Miso Che ne pensi di Community Manager per Google? In caso contattami ;) http://t.co/tBQ2e0N9 #stage #casaeditrice 1 week ago
- @MarcoEscher chi si rivede! In bocca al lupo per il tuo cammino e chissà che magari un giorno tu non mi raggiunga in Google! ;) 2 weeks ago
- @pigatss you should give one to me because I hosted you for lunch at the Googleplex :) #volunia 3 weeks ago
- Avete visto quante posizioni per italiani sono aperte in Google a Dublino? http://t.co/ZlUfZ4uA 2012-01-10
- @thetarro congratulations, I gained 4 kg in 4 weeks :( 2012-01-08
- More updates...
Posting tweet...


