Algorithms and Data Structures in TypeScript: A Complete Guide for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Algorithms and Data Structures in TypeScript: Where to Start for Beginners

Understanding algorithms and data structures is the foundation that distinguishes a professional developer from an amateur. If you are just starting your journey in programming with TypeScript, this article will help you understand the key concepts. We will cover basic data structures (arrays, stack, queue) and simple algorithms (linear and binary search), and also show how to implement them in TypeScript with strict typing.



1. Arrays and Working with Them in TypeScript

An array is the simplest and most frequently used data structure. In TypeScript, arrays can be strictly typed, which helps avoid errors at the compilation stage.

// Declaring a typed array of numbersconst numbers: number[] = [5, 12, 8, 130, 44];

// Array of stringsconst fruits: Array<string> = ['apple', 'banana', 'cherry'];

// Access by indexconsole.log(numbers[0]); // 5

// Adding an element to the endnumbers.push(100);

// Removing the last elementconst last = numbers.pop();

Important: TypeScript checks types at compile time. If you try to add a string to an array of numbers, the compiler will throw an error. This makes the code more reliable.



2. Stack and Queue: LIFO and FIFO Principles

A stack works on the LIFO principle (Last In, First Out). A queue works on the FIFO principle (First In, First Out).



Implementing a Stack in TypeScript

class Stack<T> {    private items: T[] = [];

push(element: T): void { this.items.push(element); }

pop(): T | undefined { return this.items.pop(); }

peek(): T | undefined { return this.items[this.items.length - 1]; }

isEmpty(): boolean { return this.items.length === 0; }}

// Usageconst stack = new Stack<number>();stack.push(10);stack.push(20);console.log(stack.pop()); // 20console.log(stack.peek()); // 10


Implementing a Queue in TypeScript

class Queue<T> {    private items: T[] = [];

enqueue(element: T): void { this.items.push(element); }

dequeue(): T | undefined { return this.items.shift(); }

front(): T | undefined { return this.items[0]; }

isEmpty(): boolean { return this.items.length === 0; }}

// Usageconst queue = new Queue<string>();queue.enqueue('task 1');queue.enqueue('task 2');console.log(queue.dequeue()); // 'task 1'

Notice the use of generics <T>. This allows you to create data structures that work with any type while maintaining strict typing.



3. Search Algorithms: Linear and Binary

Searching is one of the most common operations. Let's look at two basic algorithms.



Linear Search

Goes through all elements of the array until it finds the target. Complexity: O(n).

function linearSearch<T>(arr: T[], target: T): number {    for (let i = 0; i < arr.length; i++) {        if (arr[i] === target) {            return i; // found, return index        }    }    return -1; // not found}

const data = [3, 7, 1, 9, 4];console.log(linearSearch(data, 9)); // 3console.log(linearSearch(data, 10)); // -1


Binary Search (only for sorted arrays)

Divides the array in half at each step. Complexity: O(log n). This is significantly faster than linear search for large arrays.

function binarySearch(arr: number[], target: number): number {    let left = 0;    let right = arr.length - 1;

while (left <= right) { const mid = Math.floor((left + right) / 2);

Blogs

Book Recommendations