Start learning free

LESSON 120 OF 674 · ARRAYS - CHALLENGES

First N Items in C#: Copying a Slice with GetRange

Practice Read the full explanation

You get a List of integers nums and a number n. Return a List with the first n numbers of nums.

Keep them in the same order. When n is 0, return an empty List.

 

Example 1:

Input: nums = [3, 8, 1, 9, 4], n = 3
Output: [3, 8, 1]
Explanation: The first 3 numbers are 3, 8 and 1.
The 9 and the 4 are left out.

Example 2:

Input: nums = [5, 6, 7], n = 0
Output: []
Explanation: No numbers are taken, so the result is empty.

Example 3:

Input: nums = [2, 4], n = 2
Output: [2, 4]
Explanation: n is the length of nums, so every number is kept.

 

Constraints:

  • nums has 1 to 10 numbers.
  • Every number is an integer from -1000 to 1000.
  • n is from 0 to the length of nums.
STEP 1 OF 2
    Ctrl + Enter to check

    C# · ARRAYS - CHALLENGES

    First N Items in C#, explained

    Problem overview

    The method FirstN receives a List<int> named nums and a number n. It returns a new list holding the first n numbers of nums, in the same order. When n is 0, the result is an empty list.

    For nums = [3, 8, 1, 9, 4] and n = 3, the result is [3, 8, 1]: the 9 and the 4 are left out. For [5, 6, 7] and n = 0 nothing is taken, so the result is empty. For [2, 4] and n = 2, every number is kept. The constraints promise that n is between 0 and the length of nums, so you never have to ask for more numbers than exist.

    Approach

    The first n numbers are a slice that starts at index 0 and is n long. A List<T> already has a method for copying such a slice: GetRange(start, count). It takes a start index and a count, not an end index, so the call is nums.GetRange(0, n). No loop is needed.

    The result is a new list. That matters here, because the task says to return a list with the numbers, not to cut down nums itself. After the call, nums still has all its numbers.

    Step by step

    1. Copy the slice. Call GetRange on nums with the start index 0 and the count n, and store the new list in a List<int> named result. With n = 0, the count is zero and the list is empty.
    2. Return result.

    The solution in C#

    class Solution {
        public List<int> FirstN(List<int> nums, int n) {
            List<int> result = nums.GetRange(0, n);
            return result;
        }
    }
    

    Running it on the three examples prints these lists, with the numbers separated by spaces (an empty list prints nothing):

    3 8 1
    
    2 4
    

    The C# details worth knowing:

    • Count, not end index. GetRange(0, n) copies n items. Because the start is 0 the two readings give the same result here, but for a start other than 0 they do not.
    • A shallow copy. For a list of int the copy is completely independent: changing result later does not touch nums.
    • An empty result is a real list. GetRange(0, 0) returns an empty List<int>, not null, so the caller can use Count and loops on it safely.
    • No clamping. C# does not shorten an oversized request. On [2, 4], GetRange(0, 3) throws an ArgumentException. The constraints keep n within the length, so this lesson never hits it, but in a program of your own you would use Math.Min(n, nums.Count) as the count.

    Edge cases and common mistakes

    • n equal to 0. The result is empty, and the code needs no special case.
    • n equal to the length. Every number is copied, and the result equals nums.
    • Using n as an end index. That is correct only when the start is 0, which is the case here. Keep the habit anyway: the second argument is how many items.
    • Returning nums itself. That would return all the numbers instead of the first n.
    • Removing the extra numbers from nums. It also changes the list the caller passed in. Copying leaves it alone.

    Testing

    • [3, 8, 1, 9, 4], n = 3 gives [3, 8, 1].
    • [5, 6, 7], n = 0 gives [].
    • [2, 4], n = 2 gives [2, 4].
    • [7], n = 1 gives [7].
    Keep going with C#500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free