← Back to Blog

F# Kata - Merging Two Lists

I recently did a C# kata where I had to write a function that merged two arrays of strings together, ensuring that the resulting array only contained distinct strings. It was straightforward to write in C#, but I kept thinking I could do it easier in F#. I was right. My code is below:


open System;

let MergeLists (list1, list2) =
    Seq.concat [ list1; list2 ]
    |> Seq.distinct

[<EntryPoint>]
let main args =
    // Given two lists of strings, merge the lists eliminating
    // any duplicate entries.
    let names1 = [ "Fred"; "Gilligan"; "Skipper" ]
    let names2 = [ "Ginger"; "Fred"; "Maryann" ]

    let uniqueList = MergeLists (names1, names2)

    uniqueList
    |> Seq.iter (fun x -> printfn "%s" x)
        
    0
← Back to Blog