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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:31:10+00:00 2026-06-08T14:31:10+00:00

This is the basic problem: I have an array of integers with possibly duplicate

  • 0

This is the basic problem: I have an array of integers with possibly duplicate elements. I need to know the indices of each element, but when I sort the array, whenever I select an element from the new array, I want to be able to reference the same element from the original array.

I am looking for a solution to the problem, or maybe a solution to the approach I am taking.

Here is an array

a = [1, 2, 3, 4, 3, 5, 2]

There are two 2’s and two 3’s, but if I’m working with the first 2 (from the left), I want to work with index 1, and if I’m working with the second 2, I want to be working with index 6. So I use a helper array to allow me to do this:

helper = [0, 1, 2, 3, 4, 5, 6]

Which I will iterate over and use to access each element from a.
I could have accomplished this with each_with_index, but the problem begins when I sort the array.

Now I have a sort order

sort_order = [2, 4, 1, 5, 3]

I use sort_by to sort a according to sort_order, to produce

sorted_a = [2, 2, 4, 1, 5, 3, 3]

You may assume all elements in the input exist in sort_order to avoid sort_by exceptions.

Now the problem is that my helper array should be updated to match the new positions. Each element should be sorted the same way as a was sorted, because it is unclear whether the first 2 in the new array was at index 1 or at index 6 of the original array.

So my new helper array might look like

new_helper = [1, 6, 3, 0, 5, 2, 4]

So if I were to go with this approach, how would I produce the new_helper array, given the original array and the sort order?

Maybe there is a better way to do this?

  • 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-06-08T14:31:14+00:00Added an answer on June 8, 2026 at 2:31 pm

    Make a list of pairs of the original data and that data’s index. Like this:

    a = [(1, 0), (2, 1), (3, 2), (4, 3), (3, 4), (5, 5), (2,6)]
    

    Sort that list (lexicographically, or just ignore the second part of the pair except to carry it along). The second item in every pair tells you where the element was in the original array.

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

Sidebar

Related Questions

Problem : This is a probably a very basic question, but how do I
OK, this is probably very basic, but I can't find the problem. I'm trying
Hi I have this basic random quote script, but I would like it to
I have a seemingly basic problem with C# arrays. I am a beginner, but
PROBLEM I have a nested PHP array that I need to populate from flat
Basic problem I have some large, but logically organised documents - and would like
I have a commit of this app up here The basic problem is, in
OK, so I know this question seems really basic, but I can't find a
I'm reading this basic tutorial on canvas elements . The (almost) in the following
I have a js.erb with this basic content: $(#currencies).html(<%= options_for_select([['Dollar', '$']])%>); And the html

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.