Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 25bd8a5

Browse files
AkashRajpurohitKent C. Dodds
authored and
Kent C. Dodds
committed
feat(largest): finds largest number in an array (#229)
Closes #228
1 parent 8e5d379 commit 25bd8a5

File tree

3 files changed

+114
-72
lines changed

3 files changed

+114
-72
lines changed

Diff for: src/index.js

+74-72
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,92 @@
1-
import reverseArrayInPlace from './reverseArrayInPlace'
2-
import isEven from './is-even'
1+
import add from './add'
2+
import arrayAverage from './array-average'
3+
import arrayFill from './array-fill'
4+
import median from './array-median'
5+
import BitwiseAnd from './BitwiseAnd'
6+
import checkPalindrome from './checkPalindrome'
7+
import contains from './contains'
38
import capitalizeFirstLetter from './convert-first-letter-to-capital'
4-
import revstring from './revstring'
5-
import initArray from './init-array'
6-
import reduce from './reduce-to-tally'
9+
import convertToRoman from './convertToRoman'
10+
import copyArrayByValue from './copyArrayByValue'
11+
import cube from './cube'
12+
import curry from './curry'
13+
import debounce from './debounce'
14+
import dec2bin from './dec2bin'
15+
import dec2hex from './dec2hex'
16+
import descendingOrder from './descending-order'
17+
import divide from './divide'
18+
import endsWith from './endsWith'
19+
import fibonacciSum from './fibonacciSum'
20+
import find from './find'
21+
import first from './first'
722
import flatten from './flatten'
8-
import getQueryStringParam from './get-query-string-param'
9-
import snakeToCamel from './snake-to-camel'
10-
import padLeft from './pad-left'
11-
import lessThan from './less-than'
12-
import randomInteger from './random-integer'
13-
import arrayFill from './array-fill'
14-
import sortObjectsArray from './sort-objects-array'
15-
import objectValuesToString from './object-values-to-string'
23+
import gcd from './gcd'
24+
import generatePassword from './generate-password'
1625
import getObjectSize from './get-object-size'
17-
import isArray from './is-array'
18-
import validateEmail from './validateEmail'
26+
import getOrdinalSuffix from './get-ordinal-suffix'
27+
import getQueryStringParam from './get-query-string-param'
28+
import getQueryStringValue from './get-query-string-value'
29+
import getMiddle from './getMiddle'
1930
import hex2rgb from './hex2rgb'
31+
import inchesToMetric from './inches-to-metric'
32+
import initArray from './init-array'
33+
import isArray from './is-array'
34+
import isEven from './is-even'
35+
import isFunction from './is-function'
2036
import isNullOrWhitespace from './is-null-or-whitespace'
37+
import isNumeric from './is-numeric'
38+
import isOdd from './is-odd'
39+
import isPrime from './is-prime'
2140
import isToday from './is-today'
22-
import startsWith from './startsWith'
41+
import largest from './largest'
42+
import last from './last'
43+
import lcm from './lcm'
44+
import isLeapYear from './leap-year'
45+
import lessThan from './less-than'
46+
import makeObjectIterable from './makeObjectIterable'
47+
import max from './max'
48+
import mod from './mod'
49+
import mode from './mode-array'
50+
import multiply from './multiply'
51+
import numberToString from './numberToString'
52+
import objectValuesToString from './object-values-to-string'
53+
import occurrences from './occurrences'
54+
import padLeft from './pad-left'
55+
import randomInteger from './random-integer'
56+
import range from './range'
57+
import reduce from './reduce-to-tally'
58+
import reduceCount from './reduceCount'
59+
import removeAccents from './remove-accents'
2360
import removeDuplicates from './remove-duplicates'
2461
import removeFalsy from './remove-falsy'
25-
import add from './add'
62+
import removeElementFromArray from './removeElementFromArray'
63+
import removeProperty from './removeProperty'
64+
import reverse from './reverse'
65+
import reverseArrayInPlace from './reverseArrayInPlace'
66+
import revstring from './revstring'
67+
import rollDice from './rollDice'
68+
import round from './round'
69+
import searchAndReplace from './search-and-replace'
70+
import shallowEqual from './shallow-equal'
71+
import slugify from './slugify'
72+
import snakeToCamel from './snake-to-camel'
73+
import sortObjectsArray from './sort-objects-array'
74+
import sqrt from './sqrt'
75+
import square from './square'
76+
import startsWith from './startsWith'
2677
import subtract from './subtract'
2778
import subtraction from './subtraction'
28-
import divide from './divide'
29-
import multiply from './multiply'
30-
import square from './square'
31-
import cube from './cube'
3279
import sum from './sum'
33-
import dec2hex from './dec2hex'
34-
import dec2bin from './dec2bin'
35-
import searchAndReplace from './search-and-replace'
36-
import sqrt from './sqrt'
37-
import toPower from './to-power'
38-
import truncate from './truncate'
39-
import mod from './mod'
40-
import shallowEqual from './shallow-equal'
4180
import swapCase from './swap-case'
42-
import endsWith from './endsWith'
43-
import round from './round'
44-
import checkPalindrome from './checkPalindrome'
45-
import isFunction from './is-function'
46-
import isOdd from './is-odd'
47-
import isNumeric from './is-numeric'
48-
import max from './max'
49-
import slugify from './slugify'
50-
import convertToRoman from './convertToRoman'
51-
import gcd from './gcd'
52-
import range from './range'
53-
import contains from './contains'
54-
import getOrdinalSuffix from './get-ordinal-suffix'
55-
import arrayAverage from './array-average'
56-
import find from './find'
57-
import median from './array-median'
58-
import timeDifference from './timeDifference'
59-
import isPrime from './is-prime'
6081
import swapElements from './swapElements'
61-
import reverse from './reverse'
62-
import removeAccents from './remove-accents'
63-
import getQueryStringValue from './get-query-string-value'
64-
import isLeapYear from './leap-year'
65-
import removeElementFromArray from './removeElementFromArray'
66-
import generatePassword from './generate-password'
6782
import tail from './tail'
68-
import makeObjectIterable from './makeObjectIterable'
69-
import fibonacciSum from './fibonacciSum'
70-
import lcm from './lcm'
71-
import occurrences from './occurrences'
72-
import getMiddle from './getMiddle'
73-
import debounce from './debounce'
74-
import curry from './curry'
75-
import textJustification from './textJustification'
76-
import removeProperty from './removeProperty'
7783
import temperatureConverter from './temperatureConverter'
78-
import last from './last'
79-
import descendingOrder from './descending-order'
80-
import reduceCount from './reduceCount'
81-
import BitwiseAnd from './BitwiseAnd'
82-
import copyArrayByValue from './copyArrayByValue'
84+
import textJustification from './textJustification'
85+
import timeDifference from './timeDifference'
8386
import timeSince from './timeSince'
84-
import first from './first'
85-
import mode from './mode-array'
86-
import rollDice from './rollDice'
87-
import inchesToMetric from './inches-to-metric'
88-
import numberToString from './numberToString'
87+
import toPower from './to-power'
88+
import truncate from './truncate'
89+
import validateEmail from './validateEmail'
8990

9091
export {
9192
reverseArrayInPlace,
@@ -176,4 +177,5 @@ export {
176177
rollDice,
177178
inchesToMetric,
178179
numberToString,
180+
largest,
179181
}

Diff for: src/largest.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default largest
2+
3+
/**
4+
* Original Source: https://stackoverflow.com/a/33498918/9713990
5+
*
6+
* This method will find the largest element in an array
7+
*
8+
* @param {Array} array - the array
9+
* @return {Number | null} - Largest number or null (in case of empty array)
10+
*/
11+
function largest(array) {
12+
if (array.length <= 0) {
13+
return null
14+
}
15+
return Math.max(...array)
16+
}
17+
18+
// This is great in case of small array but in large size array it fails throwing this error
19+
// RangeError: Maximum call stack size exceeded
20+
// In case of large array size (eg. 10000000) we can use this
21+
22+
/*
23+
* return array.sort().pop()
24+
*
25+
* sorts the array in ascending order and returns the last item (largest) using * pop() function
26+
*/

Diff for: test/largest.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import test from 'ava'
2+
import { largest } from '../src'
3+
4+
test('gets largest number from array', t => {
5+
const array = [23, 45, 25, 67, 24, 86, 12, 94, 37]
6+
const largestItem = largest(array)
7+
t.deepEqual(largestItem, 94)
8+
})
9+
10+
test('return null in case of empty array', t => {
11+
const array = []
12+
const largestItem = largest(array)
13+
t.deepEqual(largestItem, null)
14+
})

0 commit comments

Comments
 (0)