Suppose we have an array of positive integers that represents the number of citations a particular researcher has conducted over a period of time.
We are required to write a JavaScript function that takes in one such array and the function should find the h-index of that researcher based on the citations data represented by the array.
H-Index:
Consider a researcher who performed N number of citations in his career. Then the researcher has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.
For example −
If the citations array is −
const arr = [1, 6, 3, 0, 5];
This data represents that the researcher has 5 papers in total and each of them had received 1, 6, 3, 0, 5 citations respectively.
And since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, the h-index is 3.
Therefore, the output for this array should be −
const output = 3;
Example
Following is the code −
const arr = [1, 6, 3, 0, 5]; const findHIndex = (arr = []) => { let possible = []; let { length: len } = arr; if (len === 0){ return 0; }; possible.length = len + 2; possible = possible.join('-').split('').map(() => 0); for (let i = 0; i < len; i ++) { let val = arr[i]; let ind = val > len ? len : val; possible[ind] += 1; } let result = 0; for (let k = len; k >= 0; k --) { result += possible[k]; if (result >= k) { return k; } } }; console.log(findHIndex(arr));
Output
Following is the console output −
3