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

  • SEARCH
  • Home
  • 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 1001337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:39:18+00:00 2026-05-16T07:39:18+00:00

I know I can remove the last element from a set: s.Remove(s.MaximumElement) But if

  • 0

I know I can remove the last element from a set:

s.Remove(s.MaximumElement)

But if I want to remove the n maximum elements… do I just execute the above n times, or is there a faster way to do that?

To be clear, this is an obvious solution:

let rec removeLastN (s : Set<'a>, num : int) : Set<'a> = 
    match num with
    | 0 -> s
    | _ -> removeLast(s.Remove(s.MinimumElement), num-1)

But it involves creating a new set n times. Is there a way to do it and only create a new set once?

  • 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-16T07:39:19+00:00Added an answer on May 16, 2026 at 7:39 am

    But it involves creating a new set n
    times. Is there a way to do it and
    only create a new set once?

    To the best of my knowledge, no. I’d say what you have a perfectly fine implementation, it runs in O(lg n) — and its concise too 🙂 Most heap implementations give you O(lg n) for delete min anyway, so what you have is about as good as you can get it.

    You might be able to get a little better speed by rolling your balanced tree, and implementing a function to drop a left or right branch for all values greater than a certain value. I don’t think an AVL tree or RB tree are appropriate in this context, since you can’t really maintain their invariants, but a randomlized tree will give you the results you want.

    A treap works awesome for this, because it uses randomization rather than tree invariants to keep itself relatively balanced. Unlike an AVL tree or a RB-tree, you can split a treap on a node without worrying about it being unbalanced. Here’s a treap implementation I wrote a few months ago:

    http://pastebin.com/j0aV3DJQ

    I’ve added a split function, which will allows you take a tree and return two trees containing all values less than and all values greater than a given value. split runs in O(lg n) using a single pass through the tree, so you can prune entire branches of your tree in one shot — provided that you know which value to split on.

    But if I want to remove the n maximum
    elements… do I just execute the
    above n times, or is there a faster
    way to do that?

    Using my Treap class:

    open Treap
    
    let nthLargest n t = Seq.nth n (Treap.toSeqBack t)
    let removeTopN n t =
        let largest = nthLargest n t
        let smallerValues, wasFound, largerValues = t.Split(largest)
        smallerValues
    
    let e = Treap.empty(fun (x : int) (y : int) -> x.CompareTo(y))
    let t = [1 .. 100] |> Seq.fold (fun (acc : Treap<_>) x -> acc.Insert(x)) e
    let t' = removeTopN 10 t
    

    removeTopN runs in O(n + lg m) time, where n is the index into the tree sequence and m is the number of items in the tree.

    I make no guarantees about the accuracy of my code, use at your own peril 😉

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

Sidebar

Related Questions

I want to know is there any way that I can remove duplicates from
How would I remove a view from this dialog? I know I can remove
How can I remove focus from submit button? I don't know why it is
Can anybody know how to remove all the indexes from database ?
i want to know how to remove underline in display listview in android, can
I want to know can we have a JPanel with a Layout other than
Okay. Say I have string '193' And I want to remove the last numbers
I want to remove an individual marker from Google map. I am using version
I need to RTRIM the last 7 characters from a result set in an
I have a source container of strings I want to remove any strings from

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.