Skip to content

Commit e6df6eb

Browse files
authored
New Algorithm: Parity Outlier (TheAlgorithms#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
1 parent 84b01ae commit e6df6eb

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Maths/ParityOutlier.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @author mrmagic2020
3+
* @description The function will find the parity outlier from an array of integers.
4+
* @see https://en.wikipedia.org/wiki/Parity_(mathematics)
5+
* @param {number[]} integers - An array of integers.
6+
* @returns {number} - The parity outlier.
7+
* @example parityOutlier([1, 3, 5, 8, 9]) = 8
8+
*/
9+
const parityOutlier = (integers) => {
10+
let oddsCount = 0 // define counter for odd number(s)
11+
let evensCount = 0 // define counter for even number(s)
12+
let odd, even
13+
14+
for (const e of integers) {
15+
if (!Number.isInteger(e)) { // detect non-integer elements
16+
return null
17+
}
18+
if (e % 2 === 0) { // an even number
19+
even = e
20+
evensCount++
21+
} else { // an odd number
22+
odd = e
23+
oddsCount++
24+
}
25+
}
26+
27+
if (oddsCount === 0 || evensCount === 0) return null // array has only odd/even number(s)
28+
if (oddsCount > 1 && evensCount > 1) return null // array has more than one even and odd number
29+
30+
return oddsCount === 1 ? odd : even
31+
}
32+
33+
export { parityOutlier }

Maths/test/ParityOutlier.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { parityOutlier } from '../ParityOutlier'
2+
3+
describe('Testing parityOutlier function', () => {
4+
it('should return the odd number in an array of even numbers', () => {
5+
expect(parityOutlier([1, 2, 16, -8848, 5126])).toBe(1)
6+
})
7+
8+
it('should return the even number in an array of odd numbers', () => {
9+
expect(parityOutlier([177, 5, 76, 1919])).toBe(76)
10+
})
11+
12+
it('should, if the given array has only one integer element, return the integer itself', () => {
13+
expect(parityOutlier([83])).toBe(83)
14+
expect(parityOutlier([54])).toBe(54)
15+
})
16+
17+
it('should, if the given array has only an odd and an even number, return the odd outlier', () => {
18+
expect(parityOutlier([1, 2])).toBe(1)
19+
expect(parityOutlier([4, 3])).toBe(3)
20+
})
21+
22+
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', () => {
23+
expect(parityOutlier([])).toBe(null)
24+
expect(parityOutlier([2])).toBe(null)
25+
expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null)
26+
expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null)
27+
expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null)
28+
expect(parityOutlier([1, 3, 5, 7, 2, 4, 6, 8])).toBe(null)
29+
})
30+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy