Leetcode

Number of Good Pairs

  • Time:O(n)
  • Space:O(101) = O(1)

C++

class Solution {
 public:
  int numIdenticalPairs(vector<int>& nums) {
    int ans = 0;
    vector<int> count(101);

    for (const int num : nums)
      ans += count[num]++;

    return ans;
  }
};