forked from llipio/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//msach22 | ||
// should return true if the input array has duplicant element values | ||
|
||
const hasDuplicates = (array) => { | ||
let map = {}; | ||
for (let i = 0; i < array.length; i++) { | ||
if (map[array[i]]) { | ||
return true; | ||
} | ||
map[array[i]] = true; | ||
}; | ||
return false; | ||
} | ||
|
||
array = [1,2,3]; | ||
|
||
module.exports = { | ||
hasDuplicates | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const expect = require('chai').expect; | ||
const duplicates = require('../solutions/16'); | ||
const hasDuplicates = duplicates.hasDuplicates; | ||
|
||
describe('check duplicates', () => { | ||
it('should return true since array has duplicates', () => { | ||
const array = [1,2,3,3,4]; | ||
const expected = true; | ||
expect(hasDuplicates(array)).eql(expected); | ||
}); | ||
it('should return false because array does not have duplicates', () => { | ||
const array = [1,2,3,4]; | ||
const expected = false; | ||
expect(hasDuplicates(array)).eql(expected); | ||
}); | ||
it('should return false because array is empty', () => { | ||
const array = []; | ||
const expected = false; | ||
expect(hasDuplicates(array)).eql(expected); | ||
}); | ||
}); |