How do I find a particular value in an array and return its index?
The syntax you have there for your function doesn't make sense (why would the return value have a member called arr
?).
To find the index, use std::distance
and std::find
from the <algorithm>
header.
int x = std::distance(arr, std::find(arr, arr + 5, 3));
Or you can make it into a more generic function:
template <typename Iter>
size_t index_of(Iter first, Iter last, typename const std::iterator_traits<Iter>::value_type& x)
{
size_t i = 0;
while (first != last && *first != x)
++first, ++i;
return i;
}
Here, I'm returning the length of the sequence if the value is not found (which is consistent with the way the STL algorithms return the last iterator). Depending on your taste, you may wish to use some other form of failure reporting.
In your case, you would use it like so:
size_t x = index_of(arr, arr + 5, 3);
How to find the array index with a value?
You can use indexOf
:
var imageList = [100,200,300,400,500];
var index = imageList.indexOf(200); // 1
You will get -1 if it cannot find a value in the array.
Javascript: How to get values of an array at certain index positions
Use Array.map
:
arr = ["a","b","c","d"]
indexes = [0,2]
const res = indexes.map(e => arr[e])
console.log(res)
Search an array and return index if a specific value is found
You should use a variable inside the for loop to tell if something was found, also you should exit for once you found something to avoid unnecesary search.
public static void main(String[ ] args)
{
final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 };
final int[ ] EMPTY = new int[0];
final int MINIMUM = 0;
final int MAXIMUM = 16;
boolean foundElement;
int target;
System.out.println("Searching for numbers in an array.");
for (target = MINIMUM; target <= MAXIMUM; target++)
{
System.out.print("\nIs " + target + " in the array? ");
foundElement = false;
for (int index = 0; (index < DATA.length && !foundElement); index++)
{
foundElement = (DATA[index] == target);
if ( foundElement )
System.out.printf("Yes! %d was found at index [%d]", target, index);
}
if (!foundElement)
System.out.printf(" Not found");
}
}
Also, you can read this to learn some other ways to achieve array item searching: 4 ways to search object in array
Creating a function to search for a value in an array and return it's position index using a loop
In the first time your if
happen you return no matter what!
If arr[i] == value
you return the index, else - you return -1
.
What you want to do is return -1
in the case that none of the array elements is found.
function getIndex(arr, value) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === value) {
return i;
}
}
return -1;
}
How to return an array with the indexes of a certain int from another array
If I understood your query correctly then you want an array with all the indices i
such that arr[i]==targetValue
. We can achieve this efficiently using any dynamic data structure. Like, use an ArrayList
and keep adding all the desired indices one by one then convert the List to an array and return it.
Something like this:
List<Integer> index = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == targetValue)
index.add( i );
}
int[] maxValue = index.stream().mapToInt(Integer::intValue).toArray();
return maxValue;
How to get value at a specific index of array In JavaScript?
You can access an element at a specific index using the bracket notation accessor.
var valueAtIndex1 = myValues[1];
On newer browsers/JavaScript engines (see browser compatibility here), you can also use the .at()
method on arrays.
var valueAtIndex1 = myValues.at(1);
On positive indexes, both methods work the same (the first one being more common). Array.prototype.at()
however allows you to access elements starting from the end of the array by passing a negative number. Passing -1
will give the last element of the array, passing -2
the second last, etc.
See more details at the MDN documentation.
Related Topics
Broken C++ Std Libraries on MACos High Sierra 10.13
How to Track Memory Allocations in C++ (Especially New/Delete)
Calling a Function Through Its Address in Memory in C/C++
How to Effectively Kill a Process in C++ (Win32)
C++ View Types: Pass by Const& or by Value
What's the Easiest Way to Generate Xml in C++
What Does 'Return *This' Mean in C++
Project Error: Unknown Module(S) in Qt: Webkitwidgets
Does Delete Work with Pointers to Base Class
_Attribute_((Weak)) and Static Libraries
Cc1Plus: Error: Unrecognized Command Line Option "-Std=C++11" with G++
Using Std::Move() When Returning a Value from a Function to Avoid to Copy
Std::Mutex Performance Compared to Win32 Critical_Section
Template Function Specialization for Integer Types
Add Library to Visual Studio 2008 C++ Project
Branchless Code That Maps Zero, Negative, and Positive to 0, 1, 2