From e6df6ebad64f4af75670e14ba87e39efdfa67940 Mon Sep 17 00:00:00 2001 From: Arnold Zhou <104749673+mrmagic2020@users.noreply.github.com> Date: Mon, 1 May 2023 16:16:48 +1000 Subject: [PATCH] New Algorithm: Parity Outlier (#1314) * [feat] New algorithm * [test] Add new test for ParityOutlier.js * [fix] Reset indentation * [fix] Reset indentation * [fix] Style changes * fix: improve code efficiency and a glitch * test: adds a new possible test case * fix: style fix * fix: delete redundant comments and else statements * [fix] style fix --- Maths/ParityOutlier.js | 33 ++++++++++++++++++++++++++++++++ Maths/test/ParityOutlier.test.js | 30 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Maths/ParityOutlier.js create mode 100644 Maths/test/ParityOutlier.test.js diff --git a/Maths/ParityOutlier.js b/Maths/ParityOutlier.js new file mode 100644 index 000000000..7642541ff --- /dev/null +++ b/Maths/ParityOutlier.js @@ -0,0 +1,33 @@ +/** + * @author mrmagic2020 + * @description The function will find the parity outlier from an array of integers. + * @see https://en.wikipedia.org/wiki/Parity_(mathematics) + * @param {number[]} integers - An array of integers. + * @returns {number} - The parity outlier. + * @example parityOutlier([1, 3, 5, 8, 9]) = 8 + */ +const parityOutlier = (integers) => { + let oddsCount = 0 // define counter for odd number(s) + let evensCount = 0 // define counter for even number(s) + let odd, even + + for (const e of integers) { + if (!Number.isInteger(e)) { // detect non-integer elements + return null + } + if (e % 2 === 0) { // an even number + even = e + evensCount++ + } else { // an odd number + odd = e + oddsCount++ + } + } + + if (oddsCount === 0 || evensCount === 0) return null // array has only odd/even number(s) + if (oddsCount > 1 && evensCount > 1) return null // array has more than one even and odd number + + return oddsCount === 1 ? odd : even +} + +export { parityOutlier } diff --git a/Maths/test/ParityOutlier.test.js b/Maths/test/ParityOutlier.test.js new file mode 100644 index 000000000..ed3307dcd --- /dev/null +++ b/Maths/test/ParityOutlier.test.js @@ -0,0 +1,30 @@ +import { parityOutlier } from '../ParityOutlier' + +describe('Testing parityOutlier function', () => { + it('should return the odd number in an array of even numbers', () => { + expect(parityOutlier([1, 2, 16, -8848, 5126])).toBe(1) + }) + + it('should return the even number in an array of odd numbers', () => { + expect(parityOutlier([177, 5, 76, 1919])).toBe(76) + }) + + it('should, if the given array has only one integer element, return the integer itself', () => { + expect(parityOutlier([83])).toBe(83) + expect(parityOutlier([54])).toBe(54) + }) + + it('should, if the given array has only an odd and an even number, return the odd outlier', () => { + expect(parityOutlier([1, 2])).toBe(1) + expect(parityOutlier([4, 3])).toBe(3) + }) + + it('should return null if the given array is empty, contains only one integer, contains non-interger elements or does not have a parity outlier', () => { + expect(parityOutlier([])).toBe(null) + expect(parityOutlier([2])).toBe(null) + expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null) + expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null) + expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null) + expect(parityOutlier([1, 3, 5, 7, 2, 4, 6, 8])).toBe(null) + }) +})