score:29

you can use a `.where` method with lambda that accepts the element index as its second parameter:

``````int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };

int[] newarray = somearray.where((e, i) => i < 5 || i >= somearray.length - 5).toarray();

foreach (var item in newarray)
{
console.writeline(item);
}
``````

output:

``````0, 1, 2, 3, 4, 14, 15, 16, 17, 18
``````

score:1

``````var result = somearray.take(5).union(somearray.skip(somearray.count() - 5).take(5));
``````

score:2

try this:

``````int[] somearray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
var firstfiveresults = somearray.take(5);
var lastfiveresults = somearray.reverse().take(5).reverse();
var result = firstfiveresults;
result = result.concat(lastfiveresults);
``````

the second reverse() reorders the numbers so you won't get 18,17,16,15,14

score:3

this should work

``````somearray.take(5).concat(somearray.skip(somearray.count() - 5)).take(5);
``````

score:4

try this:

``````var result = somearray.where((a, i) => i < 5 || i >= somearray.length - 5);
``````

score:6

in case you are not playing code puzzles with your co-workers, but just want to create a new array with your criteria, i wouldn't do this with queries at all, but use array.copy.

there are three distinct cases to consider:

• the source array has fewer than 5 items
• the source array has 5 to 9 items
• the source array has 10 or more items

the third one is the simple case, as the first and last 5 elements are distinct and well defined.

the other two require more thought. i'm going to assume you want the following, check those assumptions:

if the source array has fewer than 5 items, you will want to have an array of 2 * (array length) items, for example [1, 2, 3] becomes [1, 2, 3, 1, 2, 3]

if the source array has between 5 and 9 items, you will want to have an array of exactly 10 items, for example [1, 2, 3, 4, 5, 6] becomes [1, 2, 3, 4, 5, 2, 3, 4, 5, 6]

a demonstration program is

``````public static void main()
{
console.writeline(string.join(", ", headandtail(new int[]{1, 2, 3})));
console.writeline(string.join(", ", headandtail(new int[]{1, 2, 3, 4, 5, 6})));
console.writeline(string.join(", ", headandtail(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})));
}

private static t[] headandtail<t>(t[] src) {
int runlen = math.min(src.length, 5);
t[] result = new t[2 * runlen];
array.copy(src, 0, result, 0, runlen);
array.copy(src, src.length - runlen, result, result.length - runlen, runlen);
return result;
}
``````

which runs in o(1);

if you are playing code puzzles with your co-workers, well all the fun is in the puzzle, isn't it?

it's trivial though.

``````src.take(5).concat(src.reverse().take(5).reverse()).toarray();
``````

this runs in o(n).

score:11

a solution with `arraysegment<>` (requires .net 4.5 (2012) or later):

``````var result = new arraysegment<int>(somearray, 0, 5)
.concat(new arraysegment<int>(somearray, somearray.length - 5, 5));
``````

and a solution with `enumerable.range`:

``````var result = enumerable.range(0, 5).concat(enumerable.range(somearray.length - 5, 5))
.select(idx => somearray[idx]);
``````

both these solution avoid iterating through the "middle" of the array (indices 5 through 13).