We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Remove Duplicates
Remove Duplicates
dmytrolypai + 0 comments This is the same problem as in String Reductions under the Recursion subdomain, or am I missing something? Thanks!
dhirajhimani + 0 comments println(scala.io.StdIn.readLine().foldLeft("")((acc:String , x) => if (acc.contains(x)) acc else acc + x))
xdavidliu + 0 comments Haskell
import qualified Data.Set as S remDupe "" _ = "" remDupe (c:cs) seen = if S.member c seen then remDupe cs seen else c:(remDupe cs $ S.insert c seen) main = do cs <- getLine putStr $ remDupe cs S.empty
lvbarbosa + 0 comments Haskel (n^2)
import Data.List (nub) main :: IO () main = interact nub
marianomacchi + 0 comments Clojure
Using recursion:
(defn remove-duplicates [input-string seen] (if (not (empty? input-string)) (let [current-char (first input-string)] (if (.contains seen (str current-char)) (remove-duplicates (rest input-string) seen) (remove-duplicates (rest input-string) (str seen current-char)))) seen)) (println (remove-duplicates (read-line) ""))
Note: from clojure 1.9, contains can be replaced with clojure.string/includes?
Using built-in functions:
(println (apply str (distinct (read-line)))
Load more conversations
Sort 11 Discussions, By:
Please Login in order to post a comment