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.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  1. Practice
  2. Functional Programming
  3. Ad Hoc
  4. Remove Duplicates
  5. Discussions

Remove Duplicates

  • Problem
  • Submissions
  • Leaderboard
  • Discussions

Sort 9 Discussions, By:

votes
  • recency
  • votes

Please Login in order to post a comment

  • dmytrolypai 3 years ago+ 1 comment

    This is the same problem as in String Reductions under the Recursion subdomain, or am I missing something? Thanks!

    8|
    Permalink
    • AbhishekVermaIIT 3 years ago+ 0 comments

      Correct ! Same problem ... with easier test cases !

      0|
      ParentPermalink
  • marianomacchi 5 months ago+ 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)))
    
    0|
    Permalink
  • dhirajhimani 12 months ago+ 1 comment
    println(scala.io.StdIn.readLine().foldLeft("")((acc:String , x) => if (acc.contains(x)) acc else acc + x))
    
    0|
    Permalink
    • dhirajhimani 12 months ago+ 0 comments

      For simplicity it could had been println(scala.io.StdIn.readLine().distinct) :D

      0|
      ParentPermalink
  • shalini_chavan05 1 year ago+ 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
    
    0|
    Permalink
  • alexprut 1 year ago+ 0 comments

    This problem is a duplicate of https://www.hackerrank.com/challenges/string-reductions/problem

    0|
    Permalink
  • abraham_alcaina 2 years ago+ 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 ""`
    
    0|
    Permalink
    • DSousa 2 years ago+ 0 comments

      The simplest one is to just use nub.

      0|
      ParentPermalink
    • tury345 2 years ago+ 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
      
      0|
      ParentPermalink
    • zylviij 2 years ago+ 0 comments
      import Data.List
      main=getLine>>=putStrLn.nub
      
      0|
      ParentPermalink
    • paulpach 2 years ago+ 0 comments

      Yes, this is a better solution:

      import Data.List
      
      main = interact nub
      
      2|
      ParentPermalink
    • davidjorna 2 years ago+ 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 []
      
      0|
      ParentPermalink
      • fheil0815 1 year ago+ 0 comments

        you forgot to keep s, in the case that it didnt occur yet:

        | otherwise = [s]++(rd str (set ++ [s]))
        
        0|
        ParentPermalink
  • Kivvio 2 years ago+ 0 comments

    I can't use clojure.string here? I got error: Caused by: java.lang.ClassNotFoundException: clojure.string

    0|
    Permalink
  • giaym 2 years ago+ 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...

    -3|
    Permalink
    • andresf 2 years ago+ 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 :)

      0|
      ParentPermalink
      • giaym 2 years ago+ 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

        -3|
        ParentPermalink
        • andresf 2 years ago+ 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?

          1|
          ParentPermalink
          • giaym 2 years ago+ 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.

            0|
            ParentPermalink
    • CKoenig 2 years ago+ 0 comments

      sad but not the end of the world - just pick another functional language from the stack

      0|
      ParentPermalink
  • wchargin 4 years ago+ 1 comment

    FYI: this is two lines using a built-in function.

    -6|
    Permalink
    • petrpoliak9 2 years ago+ 0 comments
      [deleted]
      0|
      ParentPermalink

No more comments

Need Help?


View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature