Skip to content

Instantly share code, notes, and snippets.

@topnotch48
Last active May 9, 2018 05:15
Show Gist options
  • Save topnotch48/02afc722e38bcdbf8535d2bda5343868 to your computer and use it in GitHub Desktop.
Save topnotch48/02afc722e38bcdbf8535d2bda5343868 to your computer and use it in GitHub Desktop.
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