Last active
September 14, 2019 05:10
-
-
Save omonk/ae2e8d05efeb627e1efbff85027206ee to your computer and use it in GitHub Desktop.
skrrrrt
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
function solution(A) { | |
const { total } = A.reduce( | |
({ position, total }, _, _idx, arr) => { | |
const val = arr[position]; | |
if (val !== -1) { | |
return { | |
position: arr[position], | |
total: total + 1, | |
}; | |
} | |
return { position, total }; | |
}, | |
{ position: 0, total: 1 } | |
); | |
return total; | |
} |
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
const limit = 1000000000; | |
function solution(A) { | |
// Get count of each number | |
const count = A.reduce( | |
(acc, curr) => ({ | |
...acc, | |
[curr]: acc[curr] ? acc[curr] + 1 : 1, | |
}), | |
{} | |
); | |
return Object.keys(count).reduce((acc, curr) => { | |
if (acc >= limit) { | |
return limit; | |
} | |
const currentIndicie = count[curr]; | |
const add = (currentIndicie * (currentIndicie - 1)) / 2; | |
return acc + add; | |
}, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment