How to Skip Specific Indexes in an Array

How to skip certain array indexes?

Use this code instead:

public static int[] sortArray(int[] array) {

int length = array.length;

for (int i = 0; i < length - 1; i++) {
int k = -1; // The index of last odd element
for (int j = 0; j < length - i; j++)
if (array[j] % 2 != 0) {
if (k >= 0 && array[k] > array[j]) {
int temp = array[k];
array[k] = array[j];
array[j] = temp;
}
k = j;
}
}

return array;
}

How to skip specific indexes in an array?

There are a few issues with your algorithm, but it boils down to this: by doing y = L.index(1) you find the first index where a 1 appears. So by doing L[y] = 0, all you can do is update the first occurence of a 1.

Finding the nth index

There is no builin to find the nth appearance and so you will have to write it.

To be consistent with list.index, I made the following index function raise a ValueError when the item is not found.

Code

def index(lst, obj, n=1):
count = 0
for index, item in enumerate(lst):
if item == obj:
count += 1
if count == n:
return index
raise ValueError('{} is not in list at least {} times'.format(obj, n))

L = [1, 2, 3, 4, 1, 2, 1, 3, 0, 4]

index = index(L, 1, n=3)

L[index] = 0

print(L)

Output

[1, 2, 3, 4, 1, 2, 0, 3, 0, 4]
Using list-comprehension

Alternatively, if all you want to do is replace the nth occurence, but do not care about its actual index, you can generate a new list with a list-comprehension and an itertools.count object.

Code

from itertools import count

def replace(lst, obj, repl, n=1):
counter = count(1)
return [repl if x == obj and next(counter) == n else x for x in lst]


L = [1, 2, 3, 4, 1, 2, 1, 3, 0, 4]

new_list = replace(L, 1, 0, n=3)
print(new_list)

Output

[1, 2, 3, 4, 1, 2, 0, 3, 0, 4]

How to exclude an array of indexes from loop

You could use the Array.includes() method.

var list = [1,2,3,4];var skipIndexes = [1,3]; 
for (var i = 0; i< list.length; i++) { if (! skipIndexes.includes(i)) { console.log(list[i]); }}

How can I skip a specific Index in an array in a for loop javascript

I want to skip index 0. How do I do that? Further I want to replace
index 0 with something else.

Just start the loop from 1 instead of 0

sportsArr[0] = "Something else"; // set the first element to something else
for(var i = 1; i < sportsArr.length; i++){
// do something
}

Skip key index from a range of array's key in php

You can get the slice of array from 5th index to rest,

$result = array_slice($array,5,count($array)-5, true);

array_slice — Extract a slice of the array

Note:

array_slice() will reorder and reset the integer array indices by
default. This behaviour can be changed by setting preserve_keys to
TRUE. String keys are always preserved, regardless of this parameter.

Demo.

How to avoid the skipping of the array index in a second array?

Loop through the Arrays as you do and increment a second counter for when a fruit matches one of the case statements. Use the counter to get the year number from the array:

var fruits, text, year, i;fruits = ["Banana", "Orange", "Apple", "Mango"];year = ["0", "1", "2", "3"];text = "<ul>";let counter = 0; // <-- define a second counterfor (i = 0; i < fruits.length; i++) {  switch (fruits[i]) {    case 'Mango':      text += "<li>" + fruits[i] + "</li>";      text += "<li><b>" + year[counter] + "</b></li>";      counter++; // <-- that only get's incremented when a case is matched      break;    case 'Pine apple':      text += "<li>" + fruits[i] + "</li>";      text += "<li><b>" + year[counter] + "</b></li>";      counter++;      break;    case 'Grape':      text += "<li>" + fruits[i] + "</li>";      text += "<li><b>" + year[counter] + "</b></li>";      counter++;      break;    case 'Banana':      text += "<li>" + fruits[i] + "</li>";      text += "<li><b>" + year[counter] + "</b></li>";      counter++;      break;  }}text += "</ul>";document.getElementById("demo").innerHTML = text;
<p id="demo"></p>


Related Topics



Leave a reply



Submit