Last active
May 9, 2018 05:15
-
-
Save topnotch48/02afc722e38bcdbf8535d2bda5343868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Array = | |
open System.Collections.Generic | |
let toMap<'TItem when 'TItem : equality> array = | |
let map = array |> Seq.fold (fun (map: Dictionary<'TItem, int>) ch -> | |
if map.ContainsKey(ch) then | |
map.[ch] <- map.[ch] + 1 | |
else | |
map.Add(ch, 1) | |
map) (new Dictionary<'TItem, int>()) | |
map | |
let replaceExact (source: string) (find: string) (replace: string) = | |
let pattern = String.Format(@"\b{0}\b", find); | |
Regex.Replace(source, pattern, replace) | |
let replaceDuplicates str = | |
match str with | |
| null | "" -> str | |
| _ -> | |
let inline replaceWithWhitespaces acc key = replaceExact acc key (String.replicate (String.length key) " "); | |
str.Split(" ", StringSplitOptions.None) | |
|> Array.toMap | |
|> fun map -> map.Keys |> Array.ofSeq |> Array.filter (fun key -> map.[key] > 1) | |
|> fun duplicates -> Array.fold replaceWithWhitespaces str duplicates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment