Mastering Big O Notation with JavaScript: A Beginner’s Guide

Drop X Out
4 min readNov 30, 2023

Understanding the efficiency of algorithms and data structures is crucial for writing efficient and scalable code. One way to analyze this efficiency is through Big O notation, which describes the performance of an algorithm in terms of its input size. In this blog, we’ll explore 50 Big O exercises using JavaScript, focusing on looping, array methods, object methods, arrays, and objects. These exercises are beginner-friendly and designed to enhance your understanding of algorithmic complexity.

Mastering Big O Notation: Exercises for Beginners

Looping

1)

function printArrayElements(arr) {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
}

This function simply prints each element of an array, demonstrating linear time complexity O(n).

2)

function printAllPairs(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
console.log(arr[i], arr[j]);
}
}
}

Here, we have nested loops that print all possible pairs of elements in the array, resulting in quadratic time complexity O(n²).

3)

function printAllTriplets(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
for (let k = 0; k < arr.length; k++) {
console.log(arr[i], arr[j], arr[k]);
}
}
}
}

Similar to the quadratic loop, this function prints all possible triplets of elements, resulting in cubic time complexity O(n³).

4)

function binarySearch(arr, target) {
let start = 0;
let end = arr.length - 1;

while (start <= end) {
let mid = Math.floor((start + end) / 2);

if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}

return -1;
}

This function demonstrates logarithmic time complexity O(log n) as it performs binary search in a sorted array.

5)

function findFirstOccurrence(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
console.log("Found at index:", i);
break;
}
}
}

Here, we find the first occurrence of a specific element and exit the loop, resulting in linear time complexity O(n).

Array Methods

6)

function filterEvenNumbers(arr) {
return arr.filter(num => num % 2 === 0);
}

The filter method is used to create a new array containing only even numbers.O(n) (linear)

7)

function doubleArrayElements(arr) {
return arr.map(num => num * 2);
}

The map method is employed to double each element in the array.O(n) (linear)

8)

function sumArrayElements(arr) {
return arr.reduce((acc, num) => acc + num, 0);
}

Using the reduce method, we find the sum of all elements in the array. O(n) (linear)

9)

function hasNumberGreaterThanTen(arr) {
return arr.some(num => num > 10);
}

The some method is utilized to check if any element in the array is greater than 10. O(n) (linear)

10)

function areAllNumbersPositive(arr) {
return arr.every(num => num > 0);
}

The every method checks if all elements in the array are positive.O(n) (linear). O(n) (linear)

Object Methods

11)

function printObjectKeys(obj) {
for (let key in obj) {
console.log(key);
}
}

This function prints all keys of an object using a for...in loop. O(n) (linear)

12)

function printObjectValues(obj) {
for (let key in obj) {
console.log(obj[key]);
}
}

Similarly, this function prints all values of an object. O(n) (linear)

13)

function printObjectEntries(obj) {
for (let [key, value] of Object.entries(obj)) {
console.log(key, value);
}
}

Here, the Object.entries method is used to print all key-value pairs of an object. O(n) (linear)

14)

function combineObjects(obj1, obj2) {
return { ...obj1, ...obj2 };
}

The spread operator is used to combine two objects into a new one. O(m + n) (linear, where m and n are the sizes of the objects being merged)

15)

function extractValues(obj) {
const { prop1, prop2 } = obj;
console.log(prop1, prop2);
}

This function demonstrates object destructuring to extract specific values. O(1) (constant)

Arrays and Objects

16)

const arrayOfObjects = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
// Add more objects as needed
];

An array of objects with name and age properties are created. N/A (not applicable)

17)

function findObjectByName(array, name) {
return array.find(obj => obj.name === name);
}

The find method is employed to locate an object in the array based on a specific property value. O(n) (linear)

18)

function filterObjectsByAge(array, age) {
return array.filter(obj => obj.age === age);
}

This function filters an array of objects based on a specific property value. O(n) (linear)

19)

function sortObjectsByAge(array) {
return array.sort((a, b) => a.age - b.age);
}

The sort method is used to sort an array of objects by a specific property. O(n log n) (Linearithmic)

20)

// Example function with linear time complexity
function linearTimeFunction(arr) {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
}

O(n) (Linear)

Must Watch Our Playlist of DSA with JS.

https://youtube.com/playlist?list=PLZWk1lAlHIsc3P014D06Q2ia3vRt-eIiZ&feature=shared

--

--

Drop X Out

In the world of Degrees ,we provide skills. TRUE SKILLS. Contact Us If you want to hire a work force with unmatched skills and dedication