Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions solutions/124.js
Original file line number Diff line number Diff line change
@@ -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,
};
25 changes: 25 additions & 0 deletions test/124.js
Original file line number Diff line number Diff line change
@@ -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);
});
});