2341. Maximum Number of Pairs in Array Easy

@problem@discussion
#Array#Hash Table#Counting



1/**
2 * [2341] Maximum Number of Pairs in Array
3 *
4 * You are given a 0-indexed integer array nums. In one operation, you may do the following:
5 * 
6 * 	Choose two integers in nums that are equal.
7 * 	Remove both integers from nums, forming a pair.
8 * 
9 * The operation is done on nums as many times as possible.
10 * Return a 0-indexed integer array answer of size 2 where answer[0] is the number of pairs that are formed and answer[1] is the number of leftover integers in nums after doing the operation as many times as possible.
11 *  
12 * Example 1:
13 * 
14 * Input: nums = [1,3,2,1,3,2,2]
15 * Output: [3,1]
16 * Explanation:
17 * Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2].
18 * Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2].
19 * Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2].
20 * No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.
21 * 
22 * Example 2:
23 * 
24 * Input: nums = [1,1]
25 * Output: [1,0]
26 * Explanation: Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [].
27 * No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.
28 * 
29 * Example 3:
30 * 
31 * Input: nums = [0]
32 * Output: [0,1]
33 * Explanation: No pairs can be formed, and there is 1 number leftover in nums.
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	1 <= nums.length <= 100
39 * 	0 <= nums[i] <= 100
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/maximum-number-of-pairs-in-array/
45// discuss: https://leetcode.com/problems/maximum-number-of-pairs-in-array/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn number_of_pairs(nums: Vec<i32>) -> Vec<i32> {
51        vec![]
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_2341() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.