3

There is an array of numbers [1,2,3,4,5,6,7,8,9,10]

I need to get all numbers from this sequence that are different from current for more than 2 items, but looped.

For example if current number is one, so new list should have everything except 9,10,1,2,3, or if current number is four so new list should be everything except 2,3,4,5,6.

Is there any technique how to make this, without creating multiple loops for items at start and at the end?

Thank you.

asked Jul 12, 2012 at 8:53
4
  • Can you provide more examples? Commented Jul 12, 2012 at 8:59
  • Added one more example, hope it's more clear now. Commented Jul 12, 2012 at 9:20
  • is the array always the same? If nto does it always loop on 10? does it always contain consecutive elements, etc. Should the order they are returned be the same as the original array or is order irrelevant? Commented Jul 12, 2012 at 9:23
  • New order should be the same, but without those numbers. Length of array and number of items is dynamic (it can be array with 25 items, or with 300). And it's always consecutive. Commented Jul 12, 2012 at 9:26

3 Answers 3

2
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 
var exclude = function (start, distance, array) { 
 var result = []; 
 for (var i = 0; i < array.length; i++) { 
 var d = Math.min( 
 Math.abs(start - i - 1), 
 Math.abs(array.length + start - i - 1)
 ) 
 if (d > distance) { 
 result.push(array[i]); 
 } 
 } 
 return result; 
} 
answered Jul 12, 2012 at 10:17
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Your method is the fastest one.
I just changed to 'd > distance' as I need items in new array to be already exluded.
1

I think this performs what you asked:

// Sorry about the name
function strangePick(value, array) {
 var n = array.length
 , i = array.indexOf(value);
 if (i >= 0) {
 // Picked number
 var result = [value];
 // Previous 2 numbers
 result.unshift(array[(i + n - 1) % n]);
 result.unshift(array[(i + n - 2) % n]);
 // Next 2 numbers
 result.push(array[(i + 1) % n]);
 result.push(array[(i + 2) % n]);
 return result;
 } else {
 return [];
 }
}

Some tests:

var array = [1,2,3,4,5,6,7,8,9,10];
console.log(strangePick(1, array)); // [9,10,1,2,3]
console.log(strangePick(4, array)); // [2,3,4,5,6]
answered Jul 12, 2012 at 9:34

Comments

1

You may use javascript array.slice:

function get_offset_sequence(arr, index, offset) {
 var result = [];
 if (index - offset < 0) {
 result = arr.slice(index - offset).concat(arr.slice(0, index + offset + 1));
 }
 else if (index + offset > arr.length - 1) {
 result = arr.slice(index - offset).concat(arr.slice(0, Math.abs(arr.length - 1 - index - offset)));
 }
 else {
 result = arr.slice(index - offset, index + offset + 1)
 }
 return result;
 }

Example of use:

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var index = 1;
var offset = 2;
for (var i=0; i < 10; i++) { console.log(i, arr[i], get_offset_sequence(arr, i, offset)) }
answered Jul 12, 2012 at 9:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.