How to Find a Particular Value in an Array and Return Its Index

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



Leave a reply



Submit