Programming Language
TypeScript
Data Structures

TypeScript Data Structures

Arrays in TypeScript

What are Arrays?

An array is a collection of elements stored in a single variable. It allows storing multiple values of the same type.


Features of an Array

  • Fixed ordering of elements.
  • Homogeneous data type (all elements are of the same type).
  • Dynamic resizing.

Array Declaration

Using Generic Array Type

let numbers: Array<number> = [1, 2, 3, 4];

Using Square Bracket

let names: string[] = ["Alice", "Bob", "Charlie"];

Accessing Elements in TypeScript Array

let fruits: string[] = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Output: Apple

Types of Array in TypeScript

Single-Dimensional (1-D) Array

let numbers: number[] = [1, 2, 3, 4];

5.2 Multi-Dimensional (2-D) Array

let matrix: number[][] = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];
console.log(matrix[1][1]); // Output: 5

Key Points of Array in TypeScript

  • Arrays in TypeScript are zero-indexed.
  • Elements can be accessed and modified using their indices.

Array Methods in TypeScript

concat()

Combines two or more arrays.

let arr1 = [1, 2];
let arr2 = [3, 4];
console.log(arr1.concat(arr2)); // Output: [1, 2, 3, 4]

pop()

Removes and returns the last element.

let numbers = [1, 2, 3];
console.log(numbers.pop()); // Output: 3

shift()

Removes and returns the first element.

let numbers = [1, 2, 3];
console.log(numbers.shift()); // Output: 1

push()

Adds elements to the end of the array.

let numbers = [1, 2];
numbers.push(3);
console.log(numbers); // Output: [1, 2, 3]

unshift()

Adds elements to the beginning of the array.

let numbers = [2, 3];
numbers.unshift(1);
console.log(numbers); // Output: [1, 2, 3]

splice()

Adds/removes elements at a specific index.

let numbers = [1, 2, 4];
numbers.splice(2, 0, 3);
console.log(numbers); // Output: [1, 2, 3, 4]

slice()

Returns a subarray.

let numbers = [1, 2, 3, 4];
console.log(numbers.slice(1, 3)); // Output: [2, 3]

toString()

Converts an array to a string.

let numbers = [1, 2, 3];
console.log(numbers.toString()); // Output: 1,2,3

join()

Joins all elements into a string with a separator.

let numbers = [1, 2, 3];
console.log(numbers.join("-")); // Output: 1-2-3

reverse()

Reverses the array elements.

let numbers = [1, 2, 3];
numbers.reverse();
console.log(numbers); // Output: [3, 2, 1]

fill()

Fills an array with a static value.

let numbers = [1, 2, 3];
numbers.fill(0);
console.log(numbers); // Output: [0, 0, 0]

indexOf()

Finds the first occurrence of a value.

let numbers = [1, 2, 3];
console.log(numbers.indexOf(2)); // Output: 1

lastIndexOf()

Finds the last occurrence of a value.

let numbers = [1, 2, 3, 2];
console.log(numbers.lastIndexOf(2)); // Output: 3

reduce()

Reduces an array to a single value.

let numbers = [1, 2, 3];
console.log(numbers.reduce((sum, num) => sum + num, 0)); // Output: 6

every()

Tests if all elements meet a condition.

let numbers = [2, 4, 6];
console.log(numbers.every(num => num % 2 === 0)); // Output: true

some()

Tests if at least one element meets a condition.

let numbers = [1, 2, 3];
console.log(numbers.some(num => num > 2)); // Output: true

Advantages of Arrays

  • Efficient data storage.
  • Random access of elements.
  • Built-in methods for manipulation.

Disadvantages of Arrays

  • Fixed size (not in dynamic arrays).
  • Requires contiguous memory allocation.

Tuples in TypeScript

What are TypeScript Tuples?

Tuples allow storing multiple values of different types in a fixed-length array.

Syntax

let tuple: [string, number] = ["Alice", 30];

Accessing Elements in a Tuple

console.log(tuple[0]); // Output: Alice

Tuple Operations

push()

tuple.push(40);
console.log(tuple); // Output: ["Alice", 30, 40]

pop()

let value = tuple.pop();
console.log(value); // Output: 40

Destructuring a Tuple

let [name, age] = tuple;
console.log(name); // Output: Alice

Set in TypeScript

Set Syntax

let set = new Set<number>();

Set Methods

set.add(value)

set.add(1);

set.has(value)

console.log(set.has(1)); // Output: true

set.delete(value)

set.delete(1);

set.clear()

set.clear();

set.size

console.log(set.size); // Output: 0

Set Iterations

set.forEach(callbackFunction[, thisArgument])

set.forEach(value => console.log(value));

For-Of Iteration

for (let value of set) {
  console.log(value);
}

Map in TypeScript

What is a TypeScript Map?

A Map holds key-value pairs, where keys can be of any type.

Map Creation

let map = new Map<string, number>();
map.set("Alice", 30);

Map Methods

map.get("Alice"); // Output: 30
map.has("Alice"); // Output: true

Iterating Map Data

map.forEach((value, key) => console.log(`${key}: ${value}`));