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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:29:05+00:00 2026-05-26T07:29:05+00:00

I got this question at an interview and at the end was told there

  • 0

I got this question at an interview and at the end was told there was a more efficient way to do this but have still not been able to figure it out. You are passing into a function an array of integers and an integer for size of array. In the array you have a lot of numbers, some that repeat for example 1,7,4,8,2,6,8,3,7,9,10. You want to take that array and return an array where all the repeated numbers are put at the end of the array so the above array would turn into 1,7,4,8,2,6,3,9,10,8,7. The numbers I used are not important and I could not use a buffer array. I was going to use a BST, but the order of the numbers must be maintained(except for the duplicate numbers). I could not figure out how to use a hash table so I ended up using a double for loop(n^2 horrible I know). How would I do this more efficiently using c++. Not looking for code, just an idea of how to do it better.

  • 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-26T07:29:05+00:00Added an answer on May 26, 2026 at 7:29 am

    In what follows:

    1. arr is the input array;
    2. seen is a hash set of numbers already encountered;
    3. l is the index where the next unique element will be placed;
    4. r is the index of the next element to be considered.

    Since you’re not looking for code, here is a pseudo-code solution (which happens to be valid Python):

    arr = [1,7,4,8,2,6,8,3,7,9,10]
    seen = set()
    l = 0
    r = 0
    while True:
      # advance `r` to the next not-yet-seen number
      while r < len(arr) and arr[r] in seen:
        r += 1
      if r == len(arr): break
      # add the number to the set
      seen.add(arr[r])
      # swap arr[l] with arr[r]
      arr[l], arr[r] = arr[r], arr[l]
      # advance `l`
      l += 1
    print arr
    

    On your test case, this produces

    [1, 7, 4, 8, 2, 6, 3, 9, 10, 8, 7]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this in an interview question -- the question was more about what
I got this question in an interview and I was not able to solve
I just got this question on a SE position interview, and I'm not quite
I just got this question on an interview and had no idea how to
I got this question from my cousin: What will be the best way to
The responses I've got to this question have solved the problem I had in
I got this question in a previous interview and couldnt do it , any
I got this question in an interview with amazon. I was asked to perform
My girlfriend got this question in an interview, and I liked it so much
i have got this question to solve from one my friends. i have searched

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.