Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Practice Time and space complexity of these examples
- //Exercise 1: Sum of All Elements
- const sumArray = (arr) => {
- let sum = 0;
- for (let i = 0; i < arr.length; i++) {
- sum += arr[i];
- }
- return sum;
- }
- console.log(sumArray([1, 2, 3, 4, 5])); // Output: 15
- //Exercise 2: Find the Maximum Element
- const findMax = (arr) => {
- let max = arr[0];
- for (let i = 1; i < arr.length; i++) {
- if (arr[i] > max) {
- max = arr[i];
- }
- }
- return max;
- }
- console.log(findMax([1, 2, 3, 4, 5])); // Output: 5
- //Exercise 3: Reverse an Array
- const reverseArray = (arr) => {
- let left = 0;
- let right = arr.length - 1;
- while (left < right) {
- [arr[left], arr[right]] = [arr[right], arr[left]]; // Swap elements
- left++;
- right--;
- }
- return arr;
- }
- console.log(reverseArray([1, 2, 3, 4, 5])); // Output: [5, 4, 3, 2, 1]
- //4. Exercise 4: Check if a String is a Palindrom
- //Write a function to check if a given string is a palindrome (reads the same forward and backward).
- const isPalindrome = (str) => {
- let left = 0;
- let right = str.length - 1;
- while (left < right) {
- if (str[left] !== str[right]) {
- return false;
- }
- left++;
- right--;
- }
- return true;
- }
- console.log(isPalindrome("racecar")); // Output: true
- console.log(isPalindrome("hello")); // Output: false
- //5. Exercise 5: Count Distinct Elements
- const countDistinct = (arr) => {
- const uniqueElements = new Set(arr);
- return uniqueElements.size;
- }
- console.log(countDistinct([1, 2, 2, 3, 4, 4, 5])); // Output: 5
- //6. Exercise 6: Two Sum Problem
- Write a function that returns the indices of two numbers in an array that add up to a given target.
- const twoSum = (arr, target) => {
- const map = new Map();
- for (let i = 0; i < arr.length; i++) {
- const complement = target - arr[i];
- if (map.has(complement)) {
- return [map.get(complement), i];
- }
- map.set(arr[i], i);
- }
- return [];
- }
- // 7. Exercise 7: Merge Two Sorted Arrays
- const mergeSortedArrays = (arr1, arr2) => {
- let i = 0, j = 0;
- const merged = [];
- while (i < arr1.length && j < arr2.length) {
- if (arr1[i] < arr2[j]) {
- merged.push(arr1[i]);
- i++;
- } else {
- merged.push(arr2[j]);
- j++;
- }
- }
- while (i < arr1.length) {
- merged.push(arr1[i]);
- i++;
- }
- while (j < arr2.length) {
- merged.push(arr2[j]);
- j++;
- }
- return merged;
- }
- console.log(mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6]
- //8. Exercise 8: Find All Pairs with a Given Sum
- Write a function that returns all unique pairs of numbers from an array that add up to a given target sum.
- javascript
- Copy code
- const findPairs = (arr, target) => {
- const pairs = [];
- const seen = new Set();
- for (let num of arr) {
- const complement = target - num;
- if (seen.has(complement)) {
- pairs.push([complement, num]);
- }
- seen.add(num);
- }
- return pairs;
- }
- console.log(findPairs([2, 4, 3, 5, 6, -1, 2, 5], 7)); // Output: [[3, 4], [5, 2], [6, 1]]
- //9. Exercise 9: Move Zeroes
- //Write a function that moves all the zeroes in an array to the end while maintaining the relative order of the non-zero elements.
- const moveZeroes = (arr) => {
- let nonZeroIndex = 0;
- for (let i = 0; i < arr.length; i++) {
- if (arr[i] !== 0) {
- arr[nonZeroIndex++] = arr[i];
- }
- }
- for (let i = nonZeroIndex; i < arr.length; i++) {
- arr[i] = 0;
- }
- return arr;
- }
- console.log(moveZeroes([0, 1, 0, 3, 12])); // Output: [1, 3, 12, 0, 0]
- //10. Exercise 10: First Unique Character in a String
- //Write a function that returns the index of the first non-repeating character in a string. If all characters are repeated, return -1.
- const firstUniqueChar = (str) => {
- const charCount = {};
- for (let char of str) {
- charCount[char] = (charCount[char] || 0) + 1;
- }
- for (let i = 0; i < str.length; i++) {
- if (charCount[str[i]] === 1) {
- return i;
- }
- }
- return -1;
- }
- console.log(firstUniqueChar("leetcode")); // Output: 0
- console.log(firstUniqueChar("loveleetcode")); // Output: 2
- console.log(firstUniqueChar("aabb")); // Output: -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement