-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_of_Good_Pairs.js
More file actions
48 lines (36 loc) · 1.02 KB
/
Number_of_Good_Pairs.js
File metadata and controls
48 lines (36 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Title : 1512. Number of Good Pairs
//Category : Array
//URL : https://leetcode.com/problems/number-of-good-pairs/
//submission: https://leetcode.com/submissions/detail/425882021/
//--------------------------------------------------
/* @param {number[]} nums
* @return {number[]}
*/
// Solution 1:
//let numIdenticalPairs = function (nums) {
//let result = 0;
//for (let i = 0; i < nums.length - 1; i++) {
//nums.slice(i + 1, nums.length).forEach((x) => nums[i] == x ? result += 1 : false);
//}
//return result;
//};
// Solution 2:
let numIdenticalPairs = function (nums) {
let result = 0;
for (let i = 0; i < nums.length - 1; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]) {
result += 1;
}
}
}
return result;
};
// Solution 3:
//let numIdenticalPairs = function (nums) {
//let result = 0;
//for (let i = 0; i < nums.length - 1; i++) {
//result += nums.slice(i + 1, nums.length).filter(x => nums[i] === x).length;
//}
//return result;
//};