From 000762ab10048322c93e16593797aa34798d5e0e Mon Sep 17 00:00:00 2001 From: sarthakgupta1907-cell Date: Fri, 6 Nov 2026 11:13:57 +0530 Subject: [PATCH] feat: add array count occurrences algorithm --- Data-Structures/Array/CountOccurrences.js | 19 ++++++++ .../Array/test/CountOccurrences.test.js | 45 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 Data-Structures/Array/CountOccurrences.js create mode 100644 Data-Structures/Array/test/CountOccurrences.test.js diff --git a/Data-Structures/Array/CountOccurrences.js b/Data-Structures/Array/CountOccurrences.js new file mode 100644 index 0000000000..1ca51f73f9 --- /dev/null +++ b/Data-Structures/Array/CountOccurrences.js @@ -0,0 +1,19 @@ +/** + * @function countOccurrences + * @description Counts the number of times a value appears in an array. + * @param {Array} arr - The input array + * @param {*} value - The value to count + * @returns {number} - The number of times value appears in arr + * @see https://en.wikipedia.org/wiki/Frequency_(statistics) + * @example countOccurrences([1, 2, 2, 3, 2], 2) => 3 + * @example countOccurrences(['a', 'b', 'a'], 'a') => 2 + * @example countOccurrences([1, 2, 3], 4) => 0 + */ +const countOccurrences = (arr, value) => { + if (!Array.isArray(arr)) { + throw new TypeError('First argument must be an array') + } + return arr.reduce((count, item) => (item === value ? count + 1 : count), 0) +} + +export { countOccurrences } diff --git a/Data-Structures/Array/test/CountOccurrences.test.js b/Data-Structures/Array/test/CountOccurrences.test.js new file mode 100644 index 0000000000..bf1daffd23 --- /dev/null +++ b/Data-Structures/Array/test/CountOccurrences.test.js @@ -0,0 +1,45 @@ +import { countOccurrences } from '../CountOccurrences.js' + +describe('countOccurrences', () => { + test('countOccurrences([1,2,2,3,2], 2) returns 3', () => { + expect(countOccurrences([1, 2, 2, 3, 2], 2)).toBe(3) + }) + + test("countOccurrences(['a','b','a'], 'a') returns 2", () => { + expect(countOccurrences(['a', 'b', 'a'], 'a')).toBe(2) + }) + + test('countOccurrences([1,2,3], 4) returns 0', () => { + expect(countOccurrences([1, 2, 3], 4)).toBe(0) + }) + + test('countOccurrences empty array returns 0', () => { + expect(countOccurrences([], 1)).toBe(0) + }) + + test('countOccurrences all same elements', () => { + expect(countOccurrences([2, 2, 2, 2], 2)).toBe(4) + }) + + test('countOccurrences with boolean values', () => { + expect(countOccurrences([true, false, true, true], true)).toBe(3) + }) + + test('countOccurrences with null values', () => { + expect(countOccurrences([null, 1, null, 2], null)).toBe(2) + }) + + test('countOccurrences does not mutate original array', () => { + const arr = [1, 2, 2, 3] + countOccurrences(arr, 2) + expect(arr).toEqual([1, 2, 2, 3]) + }) + + test('throws TypeError for non-array input', () => { + expect(() => countOccurrences('123', 1)).toThrow(TypeError) + }) + + test('throws TypeError for null input', () => { + expect(() => countOccurrences(null, 1)).toThrow(TypeError) + }) +})