Skip to content

Instantly share code, notes, and snippets.

@omonk
Last active September 14, 2019 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omonk/ae2e8d05efeb627e1efbff85027206ee to your computer and use it in GitHub Desktop.
Save omonk/ae2e8d05efeb627e1efbff85027206ee to your computer and use it in GitHub Desktop.
skrrrrt
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;
}
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