Class Solution
java.lang.Object
g2301_2400.s2341_maximum_number_of_pairs_in_array.Solution
2341 - Maximum Number of Pairs in Array.<p>Easy</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you may do the following:</p>
<ul>
<li>Choose <strong>two</strong> integers in <code>nums</code> that are <strong>equal</strong>.</li>
<li>Remove both integers from <code>nums</code>, forming a <strong>pair</strong>.</li>
</ul>
<p>The operation is done on <code>nums</code> as many times as possible.</p>
<p>Return <em>a <strong>0-indexed</strong> integer array</em> <code>answer</code> <em>of size</em> <code>2</code> <em>where</em> <code>answer[0]</code> <em>is the number of pairs that are formed and</em> <code>answer[1]</code> <em>is the number of leftover integers in</em> <code>nums</code> <em>after doing the operation as many times as possible</em>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [1,3,2,1,3,2,2]</p>
<p><strong>Output:</strong> [3,1]</p>
<p><strong>Explanation:</strong></p>
<p>Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2].</p>
<p>Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2].</p>
<p>Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2].</p>
<p>No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [1,1]</p>
<p><strong>Output:</strong> [1,0]</p>
<p><strong>Explanation:</strong> Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [].</p>
<p>No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums = [0]</p>
<p><strong>Output:</strong> [0,1]</p>
<p><strong>Explanation:</strong> No pairs can be formed, and there is 1 number leftover in nums.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 100</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
numberOfPairs
public int[] numberOfPairs(int[] nums)
-