mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-11-09 22:08:58 +01:00
✨ Add "find-outlier-number" challenge
This commit is contained in:
parent
dd075269f2
commit
b06df550bb
11
challenges/find-outlier-number/README.md
Normal file
11
challenges/find-outlier-number/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# find-outlier-number
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) at 5 July 2020.
|
||||
|
||||
## Instructions :
|
||||
|
||||
You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer `N`. Write a function that takes the array as an argument and returns this "outlier" `N`.
|
||||
|
||||
## Examples :
|
||||
|
||||
See the `input-output.json` file for examples of input/output.
|
26
challenges/find-outlier-number/input-output.json
Normal file
26
challenges/find-outlier-number/input-output.json
Normal file
@ -0,0 +1,26 @@
|
||||
[
|
||||
{
|
||||
"input": [[2, 4, 0, 100, 4, 11, 2602, 36]],
|
||||
"output": 11
|
||||
},
|
||||
{
|
||||
"input": [[160, 3, 1719, 19, 11, 13, -21]],
|
||||
"output": 160
|
||||
},
|
||||
{
|
||||
"input": [[0, 1, 2]],
|
||||
"output": 1
|
||||
},
|
||||
{
|
||||
"input": [[1, 2, 3]],
|
||||
"output": 2
|
||||
},
|
||||
{
|
||||
"input": [[2, 6, 8, 10, 3]],
|
||||
"output": 3
|
||||
},
|
||||
{
|
||||
"input": [[1, 1, 0, 1, 1]],
|
||||
"output": 0
|
||||
}
|
||||
]
|
0
challenges/find-outlier-number/solutions/.gitkeep
Normal file
0
challenges/find-outlier-number/solutions/.gitkeep
Normal file
@ -0,0 +1,4 @@
|
||||
# typescript-outlier - find-outlier-number
|
||||
|
||||
Programming language : TypeScript
|
||||
Created by [@Divlo](https://github.com/Divlo) at 5 July 2020.
|
@ -0,0 +1,26 @@
|
||||
interface NumberObject {
|
||||
value: number
|
||||
index: number
|
||||
}
|
||||
|
||||
function isOdd (number: number): boolean {
|
||||
return number % 2 !== 0
|
||||
}
|
||||
|
||||
function solution (numbers: number[]): number {
|
||||
const oddNumbers: NumberObject[] = []
|
||||
const evenNumbers: NumberObject[] = []
|
||||
|
||||
numbers.forEach((number, index) => {
|
||||
const numberObject: NumberObject = { value: number, index }
|
||||
return isOdd(number)
|
||||
? oddNumbers.push(numberObject)
|
||||
: evenNumbers.push(numberObject)
|
||||
})
|
||||
|
||||
const isValueThatDiffersFromOthers =
|
||||
oddNumbers.length === 1 ? oddNumbers[0] : evenNumbers[0]
|
||||
return isValueThatDiffersFromOthers.value
|
||||
}
|
||||
|
||||
export default solution
|
Loading…
Reference in New Issue
Block a user