diff --git a/solutions/124.js b/solutions/124.js new file mode 100644 index 0000000..8af3934 --- /dev/null +++ b/solutions/124.js @@ -0,0 +1,38 @@ +// Natalie Lockhart +// Find the first non-duplicate value in an array + +const solution = (arr) => { + + if (arr.length == 0) { + return null; + } + + if (arr.length == 1){ + return arr[0]; + } + + function checkDuplicates(testArr, compareNum){ + for (a = 0; a < testArr.length; a++){ + if(compareNum == testArr[a]){ + return null; + } + else if(a == testArr.length-1){ + return compareNum; + } + } + } + + for (i = 0; i < arr.length; i++){ + var testArr = arr.slice(); + testArr.splice(i,1); + var result = checkDuplicates(testArr, arr[i]); + if(result != null){ + return result; + } + } + return null; +} + +module.exports = { + solution, +}; diff --git a/test/124.js b/test/124.js new file mode 100644 index 0000000..9887958 --- /dev/null +++ b/test/124.js @@ -0,0 +1,25 @@ +const expect = require('chai').expect; +let solution = require('../solutions/124').solution; + +describe('first non-duplicate number in an 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 null for [1,1,2,2,3,3]', () => { + const result = solution([1,1,2,2,3,3]); + expect(result).to.equal(null); + }); + it('should return 2 for [1,1,2,3,3,4,4,5,5]', () => { + const result = solution([1,1,2,3,3,4,4,5,5]); + expect(result).to.equal(2); + }); + it('should return 2 for [1,1,2,3]', () => { + const result = solution([1,1,2,3]); + expect(result).to.equal(2); + }); +}); \ No newline at end of file