LeetCode: Count Nice Pairs In An Array Solution

1/**
2 * @param {number[]} nums
3 * @return {number}
4 */
5var countNicePairs = function (nums) {
6 const getRev = num => Number(String(num).split("").reverse().join(""))
7 return (
8 Array.from(
9 nums
10 .map(num => num - getRev(num))
11 .reduce((acc, el) => acc.set(el, (acc.get(el) || 0) + 1), new Map())
12 .values()
13 )
14 .filter(x => x > 1)
15 .map(x => (x * (x - 1)) / 2)
16 .reduce((acc, el) => acc + el, 0) %
17 (1e9 + 7)
18 )
19}

Comments

Loading comments...

Tags

leetcode

array

hash table

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Sentence Similarity III

Apr 3, 2021

Previous Post

LeetCode: Palindrome Linked List

Apr 3, 2021

Yeah I skipped the follow-up

HoningJS

Search Posts