Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6845041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:27:05+00:00 2026-05-27T00:27:05+00:00

I am a beginner to Haskell. I just wonder how to implement a function

  • 0

I am a beginner to Haskell. I just wonder how to implement a function to remove the repeat element from an array. for example, [1,1,1,3,4,2,2,3], the result should be [1,3,4,2]. I don’t want to use some exist functions like element and implement this by using recursion. My idea is to compare x:xs, if x is a repeat element then do the recursion, else rerun the function. Is that correct and how to implement this by code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:27:06+00:00Added an answer on May 27, 2026 at 12:27 am

    If you cannot assume any ordering between the elements (i.e. you don’t know if it’s an instance of Ord), then you must use nub like one poster has already mentioned. Unfortunately this is O(n^2).

    If your elements implement Ord, then you can sort the list in O(nlog(n)) and then remove adjacent elements recursively (which adds just O(n) to the overall runtime). Something like this:

    remove_dups :: (Ord a, Eq a) => [a] -> [a]
    remove_dups xs = remove $ sort xs
      where
        remove []  = []
        remove [x] = [x]
        remove (x1:x2:xs)
          | x1 == x2  = remove (x1:xs)
          | otherwise = x1 : remove (x2:xs)
    

    Pretty interesting problem. We often need to do this sort of thing. =)

    Edit

    I didn’t notice the result you gave is not in non-decreasing order. The above code will produce [1,2,3,4] instead, which may not be what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner in Haskell so please bear with me. (Just started learning
I'm playing around with beginner Haskell, and I wanted to write an average function.
A beginner question, bear with me: I'm just wondering under what circumstances one should
I am a beginner interested in Haskell, and I have been trying to implement
Beginner help needed :) I am doign an example form a php book which
What database should a beginner use in the C language? Can I use MySQL?
Beginner question here. I'm going to make a Jquery function that is used to
I'm just learning Haskell, and trying to figure out the most idiomatic way to
Beginner here See - http://www.w3schools.com/js/tryit.asp?filename=try_dom_tablerow_cells In this example, when change content is pressed, new
Level: Beginner In the following code my 'samePoint' function returns False where i am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.