Find the D-ids of the K most frequent customers

Updated

In Doraland, people have unique Identity Numbers called D-id. Doraemon owns the most popular gadget shop in Doraland. Since his gadgets are in high demand and he has only K gadgets left he has decided to sell his gadgets to his most frequent customers only. N customers visit his shop and D-id of each customer is given in an array array[ ]. In case two or more people have visited his shop the same number of time he gives priority to the customer with higher D-id. Find the D-ids of people he sells his K gadgets to.

Given 1<=N<=10000

Example 1 :
Input :
N = 6
array[] = {1, 1, 1, 2, 2, 3}
K = 2
Output : 1 2
Explanation :
Customers with D-id 1 and 2 are most frequent.

Approach :

  1. Firstly we hash the given array to get frequency of D-ids
  2. Then we use a 2D arraylist for frequency hashing, where we store the values having particular indices in a single arraylist
  3. Then we traverse this arraylist from bottom to get the k most frequent D-ids

Implementation :

import java.io.*;
import java.util.*;

class Main {
    public static ArrayList<Integer>
    TopK(ArrayList<Integer> array, int k)
    {
        int i, j = 0;
        int arr[] = new int[10002];
        for (i = 0; i < 10002; i++)
            arr[i] = 0;

        for (i = 0; i < array.size(); i++)
            arr[array.get(i)]++;

        ArrayList<ArrayList<Integer> > res =  
                  new ArrayList<ArrayList<Integer> >(100002);
        for (i = 0; i < 100002; i++) {
            ArrayList<Integer> temp
                = new ArrayList<Integer>();
            res.add(temp);
        }
        for (i = 0; i < 10002; i++) {
            if (arr[i] != 0) {
                res.get(arr[i]).add(i);
            }
        }

        ArrayList<Integer> ans = new ArrayList<Integer>();
        for (i = 100001; i >= 0; i--) {
            for (int p = res.get(i).size() - 1; p >= 0; p--) {
                if (j != k)
                    ans.add(res.get(i).get(p));
                else
                    break;
                j++;
            }
        }
        return ans;
    }
    public static void main(String[] args)
    {
        ArrayList<Integer> array = new ArrayList<>();
        array.add(1);
        array.add(1);
        array.add(1);
        array.add(2);
        array.add(2);
        array.add(3);
        int k = 2;
        ArrayList<Integer> res = TopK(array, k);
        for (int c : res) {
            System.out.print(c + " ");
        }
    }
}

Time Complexity : O(N), where N is the size of given array

Space Complexity : O(N), where N is the size of given array

Comment
Next tutorial
Mithlesh Upadhyay Published 26 Nov 2026 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials