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 591003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:35:22+00:00 2026-05-13T15:35:22+00:00

Ive always been a bit confused on this, possibly due to my lack of

  • 0

Ive always been a bit confused on this, possibly due to my lack of understanding in compilers. But lets use python as an example. If we had some large list of numbers called numlist and wanted to get rid of any duplicates, we could use a set operator on the list, example set(numlist). In return we would have a set of our numbers. This operation to the best of my knowledge will be done in O(n) time. Though if I were to create my own algorithm to handle this operation, the absolute best I could ever hope for is O(n^2).

What I don’t get is, what allows a internal operation like set() to be so much faster then an external to the language algorithm. The checking still needs to be done, don’t they?

  • 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-13T15:35:22+00:00Added an answer on May 13, 2026 at 3:35 pm

    You can do this in O(n) in any language, basically as:

    # Get min and max values O(n).
    
    min = oldList[0]
    max = oldList[0]
    for i = 1 to oldList.size() - 1:
        if oldList[i] < min:
            min = oldList[i]
        if oldList[i] > max:
            max = oldList[i]
    
    # Initialise boolean list O(n)
    
    isInList = new boolean[max - min + 1]
    for i = min to max:
        isInList[i] = false
    
    # Change booleans for values in old list O(n)
    
    for i = 0 to oldList.size() - 1:
        isInList[oldList[i] - min] = true
    
    # Create new list from booleans O(n) (or O(1) based on integer range).
    
    newList = []
    for i = min to max:
        if isInList[i - min]:
            newList.append (i)
    

    I’m assuming here that append is an O(1) operation, which it should be unless the implementer was brain-dead. So with k steps each O(n), you still have an O(n) operation.

    Whether the steps are explicitly done in your code or whether they’re done under the covers of a language is irrelevant. Otherwise you could claim that the C qsort was one operation and you now have the holy grail of an O(1) sort routine 🙂

    As many people have discovered, you can often trade off space complexity for time complexity. For example, the above only works because we’re allowed to introduce the isInList and newList variables. If this were not allowed, the next best solution may be sorting the list (probably no better the O(n log n)) followed by an O(n) (I think) operation to remove the duplicates.

    An extreme example, you can use that same extra-space method to sort an arbitrary number of 32-bit integers (say with each only having 255 or less duplicates) in O(n) time, provided you can allocate about four billion bytes for storing the counts.

    Simply initialise all the counts to zero and run through each position in your list, incrementing the count based on the number at that position. That’s O(n).

    Then start at the beginning of the list and run through the count array, placing that many of the correct value in the list. That’s O(1), with the 1 being about four billion of course but still constant time 🙂

    That’s also O(1) space complexity but a very big “1”. Typically trade-offs aren’t quite that severe.

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

Sidebar

Related Questions

I've always been a bit confused about how STL containers (vector, list, map...) store
So - I've been using this method of file uploading for a bit, but
From this site: http://www.catalinzima.com/?page_id=14 I've always been confused about how the depth map is
I've been Google'ing a bit around for this, but without getting any real answer.
I've always been bugged a bit by the lack of accuracy I see bench
Ive always been against RAD controls because I wanted to do everything grammatically in
I've always been in the habit of naming my threads like this because I
It seemed like this question should have been asked before, but searching found nothing.
I've looked quite a bit, but I haven't been able to find a good
Ok, I know this has been asked a thousand times before, but no conclusive

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.