How to access array in circular manner in JavaScript

Asked
Active3 hr before
Viewed126 times

6 Answers

javascriptaccessarray
90%

Given an array arr of a length n and a value val stored in it that will be obtained through an access index i, the circular manner, and safer way, to access the array, disregarding the value and sign of i, would be:,Answering to the main question, someone can access an array in a circular manner using modular arithmetic. That can be achieved in JavaScript with the modulus operator (%) and a workaround.,This little trick is necessary -- someone can not use the modulus result straightforwardly -- because JavaScript always evaluates a modulus operation as the remainder of the division between dividend (the first operand) and divisor (the second operand) disconsidering their signs but assigning to the remainder the sign of the dividend. That behavior does not always result in the desired "wrap around" effect of the modular arithmetic and could result in a wrong access of a negative position of the array., Unfortunately that won't work for negative values of i. The result -- remainder of the division -- will be different from the result expected from modular arithmetic. For example in JS (-1 % 10 + 10) % 10 evaluates to 9, but Math.abs(-1 % 10) evaluates to 1. – wwgoncalves Feb 10 at 19:06

Given an array arr of a length n and a value val stored in it that will be obtained through an access index i, the circular manner, and safer way, to access the array, disregarding the value and sign of i, would be:

let val = arr[(i % n + n) % n];
load more v
88%

The formula you see is part of modular arithmetic. I plan digging into this stuff to see what practical things for our coding I can find! Tell me in the comments if you want more of these techniques!,Nice 👍🏻 easy to understand and nicely crafted keep posting stuffs like this ✌🏻🙌🏻,That is not matter of the index you try to access, it always gives an existing next element. ,We could hardcode it with a ternary or switch or if/else but we don't have to. Here's my solution using the circular array access:

const arr = ['a', 'b', 'c']

//REALITY
arr[0] //=> 'a'
arr[1] //=> 'b'
arr[2] //=> 'c'
arr[3] //=> undefined
arr[4] //=> undefined
arr[5] //=> undefined
arr[6] //=> undefined
arr[7] //=> undefined

// GOAL: 
arr[0] //=> 'a'
arr[1] //=> 'b'
arr[2] //=> 'c'
arr[3] //=> `a`
arr[4] //=> `b`
arr[5] //=> `c`
arr[6] //=> `a`
arr[7] //=> `b`
load more v
72%

Answering to the main question, someone can access an array in a circular manner using modular arithmetic. That can be achieved in JavaScript with the modulus operator (%) and a workaround.,Simply using modulus operator you can access array in circular manner.,Given an array arr of a length n and a value val stored in it that will be obtained through an access index i, the circular manner, and safer way, to access the array, disregarding the value and sign of i, would be:,I want to access that like in JavaScript, any ideas?

I have an array like [A,B,C,D]. I want to access that array within a for loop like as

var arr = [A, B, C, D];

var len = arr.len;
for (var i = 0; i < arr.len; i++) {
   0 - A, B, C
   1 - B, C, D
   2 - C, D, A
   3 - D, A, B
}
load more v
65%

Find the Longest Increasing Subsequence in Circular manner,Recent articles on circular array.,Implementation of Deque using circular array, For example, consider 6 people A B C D E F and given name as ‘D’. People sitting in a circular manner starting from D are D E F A B C.A simple solution is to create an auxiliary array of size 2*n and store it in another array. For example for 6 people, we create below the auxiliary array. A B C D E F A B C D E F Now for any given index, we simply print n elements starting from it. For example, we print the following 6. A B C D E F A B C D E F 

load more v
75%

I want to make a function that takes two parameters, an array (1 indexed not 0 indexed), and an integer which is the position of the element that my function should return.,If the 2nd parameter is bigger than the 1 indexed array length, I want it to start from the beginning, kinda like in a circle.,Taking the modulus of two numbers give you the remainder. So if you have an array of size 4 and you increment your counter and get 5 you actually want the first element not the fifth element that doesn't exist. That could be achieved by doing:,you want to look at the % operator, something like

I hope the examples below are descriptive enough.

[a, b, c, d], 1 - > should
return a;
[a, b, c, d], 5 - > should
return a;
[a, b, c, d], 4 - > should
return d;
[a, b, c, d], 6 - > should
return b;
load more v
40%

If you have an array that is circular, i.e. the next element of the last element is the first element.,However if curr == 0 , i.e. it is the first element, then (curr + N — 1) % N == (N — 1) % N == N — 1 , i.e. the index of the last element of the array.,if current is not the first element of the array, then (curr + N — 1) % N == (curr — 1) % N + N % N == curr — 1,if curr is not the last element then (curr + 1) % N == curr + 1, hence the next index, however if (curr + 1) == N, i.e. the current element is the last index, then (curr + 1) % N == 0, therefore the next element will be the first element of the array.

To find the next and the previous index we are going to rely of the modulus operator.

if N is the array element, curr is the current index in a 0 - index array, thennext = (curr + 1) % N
load more v

Other "javascript-access" queries related to "How to access array in circular manner in JavaScript"