Reduce Array Size to The Half

MediumGreedygreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array arr of even length. Choose a set of distinct values and delete every occurrence of each chosen value from the array. Return the minimum size of such a set so that at least half of the elements of arr are deleted.

Examples

Input: arr = [4,4,4,1,1,2,2,3]
Output: 2
Removing all 4s deletes 3 of 8 elements, which is not enough; also removing the 1s deletes 5.
Input: arr = [6,6,6,6]
Output: 1
Removing 6 empties the array.

Constraints

  • 2 <= arr.length <= 10^5
  • `arr.length` is even
  • 1 <= arr[i] <= 10^5

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems