Start learning free

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

Long Words Only in TypeScript: Filtering an Array

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

    TYPESCRIPT · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Long Words Only in TypeScript, explained

    Problem overview

    The method longWords receives an array of strings, words, and a number n. It returns a new array with the words that have more than n characters, in their original order.

    With words = ["apple", "kiwi", "banana", "fig"] and n = 4, the result is ["apple", "banana"]. "apple" has 5 characters and "banana" has 6. "kiwi" has exactly 4, which is not more than 4, so it is left out. When no word is long enough, the result is the empty array [].

    Approach

    This is a filter: look at each word once, keep it if it passes a test, skip it otherwise. The kept words go into a new array, and words itself is never changed. Because the loop meets the words from first to last and each kept word is added at the end, the order of words carries over to the result without any extra work.

    The test is word.length > n. The comparison is strict: a word with exactly n characters fails it. Writing >= would keep "kiwi" in the first example.

    Step by step

    1. Create the result array. Declare result as an empty string[]. It starts empty because nothing has passed the test yet.
    2. Loop over the words. A for...of loop with const word gives you one word per round. const is fine because each round gets a fresh variable.
    3. Test the length. Inside the loop, an if compares word.length with n.
    4. Keep the word. Inside the if, push adds the word at the end of result.
    5. Return after the loop. Returning inside the loop would end the method after the first word.

    The solution in TypeScript

    class Solution {
        longWords(words: string[], n: number): string[] {
            let result: string[] = [];
            for (const word of words) {
                if (word.length > n) {
                    result.push(word);
                }
            }
            return result;
        }
    }
    

    A few details:

    • length is a property. Write word.length with no parentheses. It counts UTF-16 code units, which for the lowercase English letters of this lesson is the same as the number of characters.
    • push changes the array in place. It adds the element to result and returns the new length, which this code ignores.
    • The annotations are for the compiler only. string[] and number are removed when TypeScript compiles to JavaScript, so nothing checks them while the code runs.
    • let versus const. The code never reassigns result, only changes its contents, so const would work too. Both pass the lesson's tests.

    The same filter can be written with the array method words.filter(word => word.length > n). The loop shows each step explicitly, which is the point of this lesson.

    Edge cases and common mistakes

    • Using >=. A word of exactly n characters would be kept. For ["kiwi", "fig"] with n = 4, the answer must be [].
    • Nothing passes. For ["hi", "to", "be"] with n = 3, the loop never pushes and the method returns [].
    • n = 0. Every word has at least one letter, so every word is kept: ["kiwi", "fig"] comes back unchanged.
    • Repeated words. Duplicates are kept, each as often as it appears. ["go", "went", "gone", "went"] with n = 3 gives ["went", "gone", "went"].
    • Returning inside the loop. The method would stop at the first word, passed or not.

    Testing

    • ["apple", "kiwi", "banana", "fig"] with n = 4 gives ["apple","banana"].
    • ["hi", "to", "be"] with n = 3 gives [].
    • ["go", "went", "gone", "went"] with n = 3 gives ["went","gone","went"].
    • ["kiwi", "fig"] with n = 0 gives ["kiwi","fig"].
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free