Last active
October 13, 2021 21:06
-
-
Save ahmet-cetinkaya/969e78f3795c0e7838209f01b0d6182d to your computer and use it in GitHub Desktop.
Leetcode Solution - 1512. Number of Good Pairs
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
public class Solution { | |
public int NumIdenticalPairs(int[] nums) { | |
var sum = 0; | |
var arr = new int[101]; | |
foreach (var num in nums) sum += arr[num]++; | |
return sum; | |
} | |
} | |
// Result: https://leetcode.com/submissions/detail/570762775/ |
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
public class Solution { | |
public int NumIdenticalPairs(int[] nums) { | |
var sum = 0; | |
var dic = new Dictionary<int, int>(); | |
foreach (var num in nums) sum += !dic.TryAdd(num, 1) ? dic[num]++ : 0; | |
return sum; | |
} | |
} | |
// Result: https://leetcode.com/submissions/detail/570760030/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment