From 0b0f3e3831a13b1e61e79ee670bd25dfeba09617 Mon Sep 17 00:00:00 2001 From: Seagullible Date: Sun, 29 Oct 2017 10:25:11 -0500 Subject: [PATCH] Adding solution to issue 126 --- solutions/126.js | 32 ++++++++++++++++++++++++++++++++ test/126.js | 21 +++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 solutions/126.js create mode 100644 test/126.js diff --git a/solutions/126.js b/solutions/126.js new file mode 100644 index 0000000..c1a95c2 --- /dev/null +++ b/solutions/126.js @@ -0,0 +1,32 @@ +//Natalie Lockhart +//Find most frequent value in array + +const solution = (arr) => { + function count(index){ + var tempCounter = 0; + for (var a = 0; a < arr.length; a++){ + if (arr[a] == arr[index]){ + tempCounter++; + } + if (tempCounter > counter){ + counter = tempCounter; + mostFrequent = arr[index]; + } + } + } + + if (arr.length == 0){ + return null; + } + + var counter = 0; + var mostFrequent = arr[0]; + for (var i = 0; i < arr.length; i++){ + count(i); + } + return mostFrequent; +}; + +module.exports = { + solution, +}; diff --git a/test/126.js b/test/126.js new file mode 100644 index 0000000..b59999c --- /dev/null +++ b/test/126.js @@ -0,0 +1,21 @@ +const expect = require('chai').expect; +let solution = require('../solutions/125').solution; + +describe('most frequent value in array', () => { + it('should return null for []', () => { + const result = solution([]); + expect(result).to.equal(null); + }); + it('should return 1 for [1]', () => { + const result = solution([1]); + expect(result).to.equal(1); + }); + it('should return 3 for [1,1,2,2,2,3,3,3,3,4]', () => { + const result = solution([1,1,2,2,2,3,3,3,3,4]); + expect(result).to.equal(3); + }); + it('should return 2 for [2,1]', () => { + const result = solution([2,1]); + expect(result).to.equal(2); + }); +}); \ No newline at end of file