common_algorithm
v1.0.3
Published
A collection of common algorithm ulities including Binary Search
Maintainers
Readme
Algorithm and Array Helper Functions for npm
This npm package offers a collection of algorithm utilities and array helper functions, including search algorithms, mathematical operations, and array transformations.
📦 Installation
npm install algorithm-helpers
🚀 Usage
const algo = require('algorithm-helpers');
🔍 Search Algorithms
binarySearch(arr, target): Finds an element in a sorted array.
linearSearch(arr, target): Finds an element in an unsorted array.
🧮 Math Operations
factorial(n): Computes the factorial of a number.
fibonacci(n): Returns the nth Fibonacci number.
sum(arr): Calculates the sum of array elements.
average(arr): Computes the average of array elements.
max(arr): Finds the maximum value in an array.
min(arr): Finds the minimum value in an array.
🧹 Array Operations
unique(arr): Removes duplicates from an array.
flatten(arr): Flattens nested arrays into a single array.
chunk(arr, size): Splits an array into sub-arrays of specified size.
groupBy(arr, key): Groups objects in an array by a specified property.
difference(arr1, arr2): Finds elements in the first array not present in the second.
intersection(arr1, arr2): Finds common elements between two arrays.
reverseString(str): Reverses a given string.
🧪 Example Usage
console.log(algo.binarySearch([1, 2, 3, 4, 5], 4)); // 3 console.log(algo.linearSearch([10, 20, 30], 20)); // 1 console.log(algo.factorial(5)); // 120 console.log(algo.fibonacci(7)); // 13 console.log(algo.sum([1, 2, 3])); // 6 console.log(algo.unique([1, 2, 2, 3])); // [1, 2, 3] console.log(algo.chunk([1,2,3,4,5], 2)); // [[1,2],[3,4],[5]]
📝 License
This project is licensed under the MIT License.
