Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Support edge case scenerio when numbers are repeated
  • Loading branch information
mazeeblanke committed May 29, 2019
commit b4b28e9e5059d2fdbeab4d8f1c9d6c44c21cd3ba
10 changes: 7 additions & 3 deletions PermCheck/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ function solution (A) {
let isPermutation = 1;
let counters = [1];

A.forEach ((elem) => {
counters[elem] = (counters[elem] || 0) + 1;
})
for (let i = 0; i < A.length; ++i) {
counters[A[i]] = (counters[A[i]] || 0) + 1;
if (counters[A[i]] > 1) {
isPermutation = 0
return isPermutation;
}
}

for (let i = 0; i < counters.length; ++i) {
if (!counters[i]) {
Expand Down
5 changes: 5 additions & 0 deletions PermCheck/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ describe('perm check', () => {
it ('returns 0 if A contains a single element', () => {
expect(permCheck([2])).toBe(0);
})

it ('returns 0 if A contains a repeated elements', () => {
expect(permCheck([2, 2])).toBe(0);
expect(permCheck([1, 1])).toBe(0);
})
})