diff --git a/solutions/34.js b/solutions/34.js index 12c1979..ace606c 100644 --- a/solutions/34.js +++ b/solutions/34.js @@ -1,6 +1,5 @@ // Daniel // Number of repeating elements in an array - const solution = (arr, k) => { let total = 0; for (i=0; i < arr.length; i++) { @@ -9,7 +8,26 @@ const solution = (arr, k) => { } } return total; - }; - module.exports = { - solution, }; + +// Maricris Bonzo +const solution1 = (array, num) => { + let i = 0; + let numOfTimes = 0; + + while (i < array.length){ + if (array[i] === num){ + numOfTimes = numOfTimes + 1; + i++; + } else { + i++; + } + } + return numOfTimes; +}; + +module.exports = { + solution, + solution1 + }; + diff --git a/test/34.js b/test/34.js index 49c4865..e2f89c8 100644 --- a/test/34.js +++ b/test/34.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; const solution = require('../solutions/34.js').solution; +const solution1 = require('../solutions/34.js').solution1; // solution = require('../yourSolution').solution; describe('return number of repeating elements', () => { @@ -15,5 +16,18 @@ describe('return number of repeating elements', () => { it('hardest case', () => { expect(solution([5, 5, 3, 2, 3, 2, 5, 5], 5)).to.equal(4); }); + it('simple case', () => { + expect(solution1([2],2)).to.equal(1); + }); + it('hard case', () => { + expect(solution1([3,2],1)).to.equal(0); + }); + it('harder case', () => { + expect(solution1([3,2,3,2],2)).to.equal(2); + }) + it('hardest case', () => { + expect(solution1([5,5,3,2,3,2,5,5],5)).to.equal(4); + }); + });