Find Kth Most Occurring Element in an Array

package org.wesome.dsalgo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class KthMostOccurring {
    static ArrayList<Integer> findKthMostOccurring(int[] arr, int kthMostFrequent) {
        HashMap<Integer, Integer> freqMap = new HashMap<>();
        for (int integer : arr) {
            freqMap.put(integer, freqMap.getOrDefault(integer, 0) + 1);
        }
        if (freqMap.size() < kthMostFrequent) {
            return new ArrayList<>();
        }
        HashMap<Integer, ArrayList<Integer>> reverseFreqMap = new HashMap<>();
        for (Map.Entry<Integer, Integer> freq : freqMap.entrySet()) {
            if (!reverseFreqMap.containsKey(freq.getValue())) {
                reverseFreqMap.put(freq.getValue(), new ArrayList<>());
            }
            reverseFreqMap.get(freq.getValue()).add(freq.getKey());
        }
        kthMostFrequent = reverseFreqMap.size() - kthMostFrequent;
        for (Map.Entry<Integer, ArrayList<Integer>> revFreqMap : reverseFreqMap.entrySet()) {
            if (kthMostFrequent == 0) {
                return revFreqMap.getValue();
            }
            kthMostFrequent--;
        }
        return new ArrayList<>();
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.wesome.dsalgo.KthMostOccurring.findKthMostOccurring;

public class KthMostOccurringTest {
    @Test
    void findKthMostOccurringTest1() {
        int arr[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
        int kthMostFrequent = 4;
        List<Integer> result = Arrays.asList(2);
        Assertions.assertTrue(result.equals(findKthMostOccurring(arr, kthMostFrequent)));
    }

    @Test
    void findKthMostOccurringTest2() {
        int arr[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
        int kthMostFrequent = 0;
        List<Integer> result = Arrays.asList();
        Assertions.assertTrue(result.equals(findKthMostOccurring(arr, kthMostFrequent)));
    }

    @Test
    void findKthMostOccurringTest3() {
        int arr[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
        int kthMostFrequent = 6;
        List<Integer> result = Arrays.asList();
        Assertions.assertTrue(result.equals(findKthMostOccurring(arr, kthMostFrequent)));
    }

    @Test
    void findKthMostOccurringTest4() {
        int arr[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
        int kthMostFrequent = 5;
        List<Integer> result = Arrays.asList(1);
        Assertions.assertTrue(result.equals(findKthMostOccurring(arr, kthMostFrequent)));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.4.1"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on