You are not logged in. Your edit will be placed in a queue until it is peer reviewed.
We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.
Required fields*
- 3fastest if the array is very longKevin– Kevin2012-11-19 19:13:50 +00:00Commented Nov 19, 2012 at 19:13
- 18Depending on your use case this could be problematic if there are duplicate values. The method described above will return the equivalent or #rindex(last occurrence of value) To get #index equivalent results, meaning the hash returning the first index of the value you'd need to do something along the lines of reversing the array before creating the hash then subtracting the returned index value from the total length of the initial array - 1. # (array.length - 1 ) - hash['b']ashoda– ashoda2013-05-30 02:49:37 +00:00Commented May 30, 2013 at 2:49
- 3Doesn't the conversion into a hash take O(n) time? I suppose if it's going to be used more than once, then hash conversion will be more performant. but for single usage, is it no different then iterating through the array?ahnbizcad– ahnbizcad2016-09-16 19:45:53 +00:00Commented Sep 16, 2016 at 19:45
- Yes, and probably worse for single use if it really matters as hash calculation won't short-circuit as quickly as a comparison.Peter DeWeese– Peter DeWeese2018-04-23 17:49:25 +00:00Commented Apr 23, 2018 at 17:49
- If you have duplicate elements, @hololeap's answer is more complete.Alex– Alex2021-12-28 14:35:23 +00:00Commented Dec 28, 2021 at 14:35
Add a comment |
How to Edit
- Correct minor typos or mistakes
- Clarify meaning without changing it
- Add related resources or links
- Always respect the author’s intent
- Don’t use edits to reply to the author
How to Format
- create code fences with backticks ` or tildes ~ ```
like so
``` - add language identifier to highlight code ```python
def function(foo):
print(foo)
``` - put returns between paragraphs
- for linebreak add 2 spaces at end
- _italic_ or **bold**
- indent code by 4 spaces
- backtick escapes
`like _so_` - quote by placing > at start of line
- to make links (use https whenever possible) <https://example.com>[example](https://example.com)<a href="https://example.com">example</a>
How to Tag
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
- complete the sentence: my question is about...
- use tags that describe things or concepts that are essential, not incidental to your question
- favor using existing popular tags
- read the descriptions that appear below the tag
If your question is primarily about a topic for which you can't find a tag:
- combine multiple words into single-words with hyphens (e.g. python-3.x), up to a maximum of 35 characters
- creating new tags is a privilege; if you can't yet create a tag you need, then post this question without it, then ask the community to create it for you
lang-rb