Remove Duplicates
Remove Duplicates
dmytrolypai + 1 comment This is the same problem as in String Reductions under the Recursion subdomain, or am I missing something? Thanks!
AbhishekVermaIIT + 0 comments Correct ! Same problem ... with easier test cases !
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)))
dhirajhimani + 1 comment println(scala.io.StdIn.readLine().foldLeft("")((acc:String , x) => if (acc.contains(x)) acc else acc + x))
dhirajhimani + 0 comments For simplicity it could had been println(scala.io.StdIn.readLine().distinct) :D
shalini_chavan05 + 0 comments let dup list= match list with |[] -> [] |head :: tail -> List.fold (fun op head -> if List.contains head op then op else head :: op )[head] tail |> List.rev let result = dup (List.ofSeq str) let op = System.String.Concat(Array.ofList(result)) printfn "%s" op
alexprut + 0 comments This problem is a duplicate of https://www.hackerrank.com/challenges/string-reductions/problem
abraham_alcaina + 5 comments Haskell
Do you have a better solution? I not 100% satisfied
removeDuplicates:: String -> String -> String removeDuplicates [] n = reverse n removeDuplicates (x:xs) result = removeDuplicates xs result' where result' = if x `elem` result then result else x:result main :: IO () main = do str <- getLine putStrLn $ removeDuplicates str ""`
DSousa + 0 comments The simplest one is to just use nub.
tury345 + 0 comments I used
filt :: [Char] -> [Char] filt "" = [] filt (x:xs) = x : (filt $ filter (/= x) xs)
or
filt s = foldl (\acc x -> acc ++ (if x `elem` acc then "" else [x])) "" s
zylviij + 0 comments import Data.List main=getLine>>=putStrLn.nub
paulpach + 0 comments Yes, this is a better solution:
import Data.List main = interact nub
davidjorna + 1 comment I used the same method, but without using "reverse:"
rd :: String -> String -> String rd [] set = set rd (s:str) set | s `elem` set = rd str set | otherwise = rd str (set ++ [s]) main :: IO () main = do str <- getLine putStrLn $ rd str []
fheil0815 + 0 comments you forgot to keep s, in the case that it didnt occur yet:
| otherwise = [s]++(rd str (set ++ [s]))
Kivvio + 0 comments I can't use clojure.string here? I got error: Caused by: java.lang.ClassNotFoundException: clojure.string
giaym + 2 comments Just wanted to say I really hate Haskell. Took me two months to figure out how to read strings and work with them...
andresf + 1 comment Two months to find getLine? It's easier to read a string from stdin in Haskell than in many mainstream languages!
Here's a link you'll find very useful: https://www.haskell.org/hoogle/
What's great about Hoogle is that you can look up a function by signature. So, say you want to know which function(s) to use to read a String from stdin. You don't know their names, but you know they do IO and that you want a String, so their type must be
:: IO String
. Type that in Hoogle and see what happens :)giaym + 1 comment I knew all that back then, problem was going from IO to the non IO datatypes needed, and how to read them when formated, millions of integers in a single line, and such, the easy challenges dont cover all the input setups
I have figured IO already though
andresf + 1 comment That is all very easy as well. Going from IO to "the non IO datatypes" is Haskell 101, without which you cannot do any real work.
Hopefully you don't hate it anymore then?
giaym + 0 comments I stil think it is a super convoluted mess and newbie unfriendly, but I can do it. Once you have data in haskell format it gets better too as it is much faster than other engines.
CKoenig + 0 comments sad but not the end of the world - just pick another functional language from the stack
wchargin + 1 comment FYI: this is two lines using a built-in function.
petrpoliak9 + 0 comments [deleted]
No more comments
Sort 9 Discussions, By:
Please Login in order to post a comment