Start learning free

LESSON 221 OF 289 · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

Long Words Only in C#: Filtering with foreach and List

Practice Read the full explanation

Given an array of strings words and an integer n, return the words that are longer than n characters.

A word must be strictly longer than n: a word with exactly n characters is left out. Keep the words in the order they have in words. If no word is long enough, the result is empty.

 

Example 1:

Input: words = ["apple", "kiwi", "banana", "fig"], n = 4
Output: ["apple", "banana"]
Explanation: apple has 5 characters and banana has 6. kiwi has exactly 4, so it is left out, and fig has only 3.

Example 2:

Input: words = ["hi", "to", "be"], n = 3
Output: []
Explanation: No word has more than 3 characters.

Example 3:

Input: words = ["go", "went", "gone", "went"], n = 3
Output: ["went", "gone", "went"]
Explanation: Every word longer than 3 characters is kept, a repeated one too, in its original order.

 

Constraints:

  • words has at least one word, and every word is made of lowercase English letters.
  • n is 0 or more.
STEP 1 OF 5
    Ctrl + Enter to check

    C# · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Long Words Only in C#, explained

    Problem overview

    The method LongWords receives a string[] called words and an int called n. It returns a List<string> with the words that have more than n characters, in their original order. A word with exactly n characters is left out, and a word that appears twice is kept twice.

    For words = ["apple", "kiwi", "banana", "fig"] and n = 4, the method returns apple and banana. The word kiwi has exactly 4 characters, so it fails the test, and fig has only 3. For ["hi", "to", "be"] and n = 3, no word is long enough and the result is an empty list.

    Approach: collect the words that pass

    This is a filter: look at each word once, keep it if it passes a test, skip it otherwise. You don't know in advance how many words will pass, so the result can't be an array of a fixed size. A List<string> starts empty and grows with Add, which suits the job.

    Visiting the words from first to last and adding each passing word at the end of the list keeps the original order without any extra work.

    Here is the first example in order. apple has 5 characters, more than 4, so it is added. kiwi has 4, which is not more than 4, so it is skipped. banana has 6 and is added, and fig has 3 and is skipped. The list ends as apple, banana. The length test reads the same for every word, so the loop body stays two lines long: a comparison and an Add.

    Step by step

    1. Create an empty List<string> named result. It holds the words that pass.
    2. Write a foreach loop with a string named word over words. It gives each word one turn.
    3. Test the length inside the loop with word.Length > n. The operator is > and not >=, because a word of exactly n characters must be left out.
    4. Add the word to result inside the if.
    5. Return result after the loop. If nothing passed, it is still an empty list, which is the right answer.

    The solution in C#

    class Solution {
        public List<string> LongWords(string[] words, int n) {
            List<string> result = new List<string>();
            foreach (string word in words) {
                if (word.Length > n) {
                    result.Add(word);
                }
            }
            return result;
        }
    }

    A few details are worth knowing:

    • On a string, Length is a property, so it is written without parentheses. An array also uses Length, while a List uses Count.
    • foreach only reads the array. It doesn't change words, so the original is untouched.
    • The method returns List<string> even though the input is an array. The two types are different, and a List is the one that can grow.

    Edge cases and common mistakes

    • The boundary. Using >= would keep kiwi in the first example. The test is strictly longer.
    • Returning inside the loop. A return there would stop at the first word that passes and give back only part of the answer.
    • n = 0. Every word has at least one letter, so with n = 0 every word is kept.
    • Nothing passes. The method returns an empty list, not null.

    Testing

    wordsnResult
    ["apple", "kiwi", "banana", "fig"]4apple, banana
    ["hi", "to", "be"]3empty list
    ["go", "went", "gone", "went"]3went, gone, went
    ["a", "bb"]0a, bb
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free