Answer: NORTHROP GRUMMAN | Off-Campus OA (2022) | Reduce Array Size to The Half
Answer · Posted Jun 2026
Approach Count the frequency of every element using a HashMap. Store all frequencies in a list. Sort the frequencies in descending order. Remove the most frequent elements first until at least half of the array is removed. Return the number of distinct elements removed. Strategy Use a Greedy approach. Always remove the element with the highest frequency first because it reduces the array size the fastest. Sorting frequencies helps process them in decreasing order. Java Code import java.util.*; class Solution ...
The full answer & interview discussion are available to premium members.
Log in Create a free account