Comments on: Find popular element in an array https://demo.ritambhara.in/find-popular-element-in-an-array/ Coding / System Design Interviews Sat, 17 Jun 2017 16:31:40 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Suresh https://demo.ritambhara.in/find-popular-element-in-an-array/#comment-1982 Sat, 17 Jun 2017 16:31:40 +0000 http://www.ritambhara.in/?p=2966#comment-1982 In reply to Suresh.

public void findElementsThatApprearNbyKTimesUsingSorting(int[] input, int k) {
if (input == null || input.length == 0)
return;
Arrays.sort(input);
int i = 0, start = 0, end = -1, n = input.length;
while (i++ < k) {
start = end + 1;
end = start + n / k – 1;
int index = findFirstOccuranceOfANumberInSortedArrayUsingBinarySearch(input, start, end, input[end]), temp = index + n / k – 1;
if (index != -1 && temp < n && input[temp] == input[end])
System.out.print(input[index] + " ");
}
System.out.println();
}
private int findFirstOccuranceOfANumberInSortedArrayUsingBinarySearch(int[] input, int start, int end, int num) {
while (start <= end) {
int mid = (start + end) / 2;
if ((mid == 0 || input[mid – 1] = num)
end = mid – 1;
else
start = mid + 1;
}
return -1;
}

]]>
By: Suresh https://demo.ritambhara.in/find-popular-element-in-an-array/#comment-1981 Sat, 17 Jun 2017 03:43:15 +0000 http://www.ritambhara.in/?p=2966#comment-1981 Could you please complete code for the binary search method. I want to see how input is partitioned and fed to binary search

]]>
By: N https://demo.ritambhara.in/find-popular-element-in-an-array/#comment-1980 Sun, 13 Mar 2016 01:12:21 +0000 http://www.ritambhara.in/?p=2966#comment-1980 it doesnt work for number at the lowest index

]]>