Related
I have an array of items :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
// or more items
I have managed to split it into chunks by 3 items per array and pushed them into an array of arrays :
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11] // <== the last one has less items than the others
]
I want to redistribute the items of the last array equally between the previous chunks :
// expected output
[
[1, 2, 3, 10],
[4, 5, 6, 11],
[7, 8, 9],
]
or even more complex like redistrbuting the items at random positions :
// expected output
[
[1, 2, 3, 10],
[4, 5, 6],
[7, 8, 9, 11],
]
so far this is what i have reached :
let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
let arrayOfChunks = [];
let amount = 3;
for (let i = 0; i < array.length; i += amount) {
const chunk = array.slice(i, i + amount);
if (chunk.length === amount) {
arrayOfChunks.push(chunk);
} else {
console.log(chunk);
// output [10,11]
}
}
return arrayOfChunks;
I tried making another loop dpending on the arrayOfChunks.length = 3 where I could redistribute the items of the last array evenly into the arrayOfChunks, but sometimes the arrayOfChunks.length = 5 which require another splitting and merging into the previous generated equal chunks.
thanks for your help :)
After chunking the array normally, pop off the last subarray if the chunk number didn't divide the original array length evenly. Then, until that last popped subarray is empty, push items to the other subarrays, rotating indicies as you go.
const evenChunk = (arr, chunkSize) => {
const chunked = [];
for (let i = 0; i < arr.length; i += chunkSize) {
chunked.push(arr.slice(i, i + chunkSize));
}
if (arr.length % chunkSize !== 0) {
const last = chunked.pop();
let i = 0;
while (last.length) {
chunked[i].push(last.shift());
i = (i + 1) % chunked.length;
}
}
console.log(chunked);
};
evenChunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], 7);
evenChunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3);
I need an algorithm to rank elements of an array in Javascript.
Example : I have an array as follows:
[79, 5, 18, 5, 32, 1, 16, 1, 82, 13]
I need to rank the entries by value. So 82 should receive rank 1, 79 rank 2 etc.
If two entries have the same value they receive the same rank and the rank for a lower value is raised.
So for this array, the new ranking array would be:
[2, 7, 4, 7, 3, 9, 5, 9, 1, 6]
How can I do this?
var arr = [79, 5, 18, 5, 32, 1, 16, 1, 82, 13];
var sorted = arr.slice().sort(function(a,b){return b-a})
var ranks = arr.map(function(v){ return sorted.indexOf(v)+1 });
console.log(ranks);
Result :
[2, 7, 4, 7, 3, 9, 5, 9, 1, 6]
If you want to be compatible with old browsers, you may have to define a shim for indexOf and for map (note that if you want to do this very fast for very big arrays, you'd better use for loops and use an object as map instead of indexOf).
This won't work with older browsers because it uses ECMAScript 5 features, but it allows you to quickly and succinctly produce an array of rankings even for very large arrays. (It doesn't use indexOf which does a linear search and thus can be slow for large arrays.)
function cmp_rnum(a,b) {
// comparison function: reverse numeric order
return b-a;
}
function index_map(acc, item, index) {
// reduction function to produce a map of array items to their index
acc[item] = index;
return acc;
}
function ranks(v) {
var rankindex = v.slice().sort(cmp_rnum).reduceLeft(index_map, Object.create(null));
// reduceLeft() is used so the lowest rank wins if there are duplicates
// use reduce() if you want the highest rank
return v.map(function(item){ return rankindex[item]+1; });
}
Example output:
> ranks([79, 5, 18, 5, 32, 1, 16, 1, 82, 13]);
[2, 7, 4, 7, 3, 9, 5, 9, 1, 6]
function rank(arr, f) {
return arr
.map((x, i) => [x, i])
.sort((a, b) => f(a[0], b[0]))
.reduce((a, x, i, s) => (a[x[1]] =
i > 0 && f(s[i - 1][0], x[0]) === 0 ? a[s[i - 1][1]] : i + 1, a), []);
}
Usage:
rank([79, 5, 18, 5, 32, 1, 16, 1, 82, 13], (a, b) => b - a);
// [2, 7, 4, 7, 3, 9, 5, 9, 1, 6]
Looks a bit ugly, but it doesn't use indexOf() or an object/map, so not only does it run a little faster, but more importantly, it respects the meaning of "same rankedness" as defined by the comparison function. If one uses indexOf() or an object, "same rankedness" can only mean a === b or String(a) === String(b).
Alternatively, use findIndex():
function rank(arr, f) {
const sorted = arr.slice().sort(f)
return arr.map(x => sorted.findIndex(s => f(x, s) === 0) + 1)
}
JavaScript ES6 simple two lines solution.
var arrayRankTransform = arr => {
const sorted = [...arr].sort((a, b) => b - a);
return arr.map((x) => sorted.indexOf(x) + 1);
};
console.log(arrayRankTransform([79, 5, 18, 5, 32, 1, 16, 1, 82, 13]));
I am not good at Javascript but in PHP it can be done quite easily the following way. Somebody good at JavaScript can come up with the relevant code.
$marks = [79, 5, 18, 5, 32, 1, 16, 1, 82, 13];
public function getRank($marks) {
$rank = 1; $count = 0; $ranks = [];
//sort the marks in the descending order
arsort($marks,1);
foreach($marks as $mark) {
//check if this mark is already ranked
if(array_key_exists($mark, $ranks)) {
//increase the count to keep how many times each value is repeated
$count++;
//no need to give rank - as it is already given
} else {
$ranks[$mark] = $i+$j;
$i++;
}
return $ranks;
}
I needed the same piece of code for an operations scheduling script I was writing. I used objects and their properties/keys, which can have any value and can be accessed whenever needed. Also, as far as I read in some articles, the search of properties in objects can be faster than search in arrays.
The script below has three simple steps:
sort the values (ascending or descending doesn't matter for the rest of the script)
find the ranks and number of occurrences for each value
replace the given values with ranks using the data from step 2
Note! The below script will not output duplicate ranks, but instead increments ranks for duplicate values/elements.
function rankArrayElements( toBeRanked ) {
// STEP 1
var toBeRankedSorted = toBeRanked.slice().sort( function( a,b ) { return b-a; } ); // sort descending
//var toBeRankedSorted = toBeRanked.slice().sort( function( a,b ) { return a-b; } ); // sort ascending
var ranks = {}; // each value from the input array will become a key here and have a rank assigned
var ranksCount = {}; // each value from the input array will become a key here and will count number of same elements
// STEP 2
for (var i = 0; i < toBeRankedSorted.length; i++) { // here we populate ranks and ranksCount
var currentValue = toBeRankedSorted[ i ].toString();
if ( toBeRankedSorted[ i ] != toBeRankedSorted[ i-1 ] ) ranks[ currentValue ] = i; // if the current value is the same as the previous one, then do not overwrite the rank that was originally assigned (in this way each unique value will have the lowest rank)
if ( ranksCount[ currentValue ] == undefined ) ranksCount[ currentValue ] = 1; // if this is the first time we iterate this value, then set count to 1
else ranksCount[ currentValue ]++; // else increment by one
}
var ranked = [];
// STEP 3
for (var i = toBeRanked.length - 1; i >= 0; i--) { // we need to iterate backwards because ranksCount starts with maximum values and decreases
var currentValue = toBeRanked[i].toString();
ranksCount[ currentValue ]--;
if ( ranksCount[ currentValue ] < 0 ) { // a check just in case but in theory it should never fail
console.error( "Negative rank count has been found which means something went wrong :(" );
return false;
}
ranked[ i ] = ranks[ currentValue ]; // start with the lowest rank for that value...
ranked[ i ] += ranksCount[ currentValue ]; // ...and then add the remaining number of duplicate values
}
return ranked;}
I also needed to do something else for my script.
The above output has the following meaning:
index - the ID of the element in the input array
value - the rank of the element from the input array
And I needed to basically 'swap the index with the value', so that I have a list of element IDs, arranged in the order of their ranks:
function convertRanksToListOfElementIDs( ranked ) { // elements with lower ranks will be first in the list
var list = [];
for (var rank = 0; rank < ranked.length; rank++) { // for each rank...
var rankFound = false;
for (var elementID = 0; elementID < ranked.length; elementID++) { // ...iterate the array...
if ( ranked[ elementID ] == rank ) { // ...and find the rank
if ( rankFound ) console.error( "Duplicate ranks found, rank = " + rank + ", elementID = " + elementID );
list[ rank ] = elementID;
rankFound = true;
}
}
if ( !rankFound ) console.error( "No rank found in ranked, rank = " + rank );
}
return list;}
And some examples:
ToBeRanked:
[36, 33, 6, 26, 6, 9, 27, 26, 19, 9]
[12, 12, 19, 22, 13, 13, 7, 6, 13, 5]
[30, 23, 10, 26, 18, 17, 20, 23, 18, 10]
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
[7, 7, 7, 7, 7, 2, 2, 2, 2, 2]
[2, 2, 2, 2, 2, 7, 7, 7, 7, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
rankArrayElements( ToBeRanked ):
[0, 1, 8, 3, 9, 6, 2, 4, 5, 7]
[5, 6, 1, 0, 2, 3, 7, 8, 4, 9]
[0, 2, 8, 1, 5, 7, 4, 3, 6, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
convertRanksToListOfElementIDs( rankArrayElements( ToBeRanked ) ):
[0, 1, 6, 3, 7, 8, 5, 9, 2, 4]
[3, 2, 4, 5, 8, 0, 1, 6, 7, 9]
[0, 3, 1, 7, 6, 4, 8, 5, 2, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
IMHO several solutions here are incorrect as they do not correctly handle values that occur after repeated values. Such followers should get the next rank. The highest rank should equal the number of unique values in the array. This solution (in PHP) is, IMHO, correct. Basically #Suresh's solution with the bugs removed.
function rank($marks){
$rank = 1; $ranks = [];
rsort($marks,SORT_NUMERIC);
foreach($marks as $mark) {
if(!isset($ranks[$mark])) {
$ranks[$mark] = $rank++;
}
}
return $ranks;
}
This should work with duplicate keys in the array
function rank(arry) {
let sorted = arry.slice().sort(function (a, b) {
return b - a
});
let currentRank = sorted.length;
let rankValue = null;
let ranks = [];
sorted.forEach(value => {
if(value !== rankValue && rankValue !==null) {
currentRank--;
}
ranks.push({value,currentRank});
rankValue = value;
});
let mapRanksToArrayValues = arry.map(function (x) {
let _rank = null;
ranks.forEach( rank => {
if(rank.value === x ) {
_rank = rank.currentRank;
return;
}
});
return _rank;
});
return mapRanksToArrayValues;
}
I created Rank_JS Pro.
<script>https://cdn.statically.io/gl/maurygta2/mquery/master/Rank Tools/rank.js</script>
Basics Methods:
var a = {
b: 2,
c: 7
}
Rank_Tools.rank(a,(pos,name,value) => {
return pos + ". "+name+" "+value;
})
// result
// rank1 = 1. c 7
// rank 2 = 2. b 2
This alternate way doesn't require the input array to be sorted:
// O(n^2)
const rank = (arr) => {
// Create a temporary array to keep metadata
// regarding each entry of the original array
const tmpArr = arr.map(v => ({
value: v,
rank: 1,
}));
// Get rid of douplicate values
const unique = new Set(arr);
// Loops through the set
for (let a of unique) {
for (let b of tmpArr) {
// increment the order of an element if a larger element is pressent
if (b.value < a) {
b.rank += 1;
}
}
}
// Strip out the unnecessary metadata
return tmpArr.map(v => v.rank);
};
console.log(rank([2600, 200, 36, 36, 400, 2, 0, 0]));
// => [1, 3, 4, 4, 2, 5, 6, 6]
console.log(rank([79, 5, 18, 5, 32, 1, 16, 1, 82, 13]));
// => [2, 7, 4, 7, 3, 8, 5, 8, 1, 6]
I had the same homework and this one works well, also it's easier to understand if you are new to this.
function rankings(arr) {
let rankingsArr = [];
for (let i = 0; i < arr.length; i++) {
var rank = 1;
for (let j = 0; j < arr.length; j++) {
if (arr[j] > arr[i]) rank++;
}
rankingsArr.push(rank);
}
return rankingsArr;
}
I want to make array that contain numbers into new array which has sum result of two by two.
For example, if there is number array like below
const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
I want to change it like below
const newArrayOne = [1, 5, 9, 13, 17]
other example is
const arrayTwo = [10, 11, 12, 13, 14, 15]
const newArrayTwo = [21, 25, 29]
How can I achieve this?
You could take flatMap and return every second sum.
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
pairs = array.flatMap((v, i, a) => i % 2 ? [] : [v + (a[i + 1] || 0)]);
console.log(pairs);
You can simply do it with for iteration.
const arrayOne = [10, 11, 12, 13, 14, 15, 17]
const result = [];
for(var i = 0; i < arrayOne.length; i+=2){
const sum = arrayOne[i] + ((i + 1 < arrayOne.length) ? arrayOne[i + 1] : 0);
result.push(sum);
}
console.log(result)
let array = [ 1, 3, 5, 7, 9, 11]
let newArray = []
for (let i =0; i < array.length; i = i + 2){
newArray.push(array[i] + array[i+1])
}
console.log(newArray)
You can use reduce:
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const output = arr.reduce((a, e, i) => (a[a.length - 1] += e, i % 2 && i !== arr.length - 1 && a.push(0), a), [0])
console.log(output)
Or, you could chunk the array, then add the elements together:
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const output = arr.reduce((a, _, i) => (i % 2 === 0 && a.push(arr.slice(i, i + 2)), a), [])
.flatMap(([a,b]) => a + b)
console.log(output)
I need an algorithm to rank elements of an array in Javascript.
Example : I have an array as follows:
[79, 5, 18, 5, 32, 1, 16, 1, 82, 13]
I need to rank the entries by value. So 82 should receive rank 1, 79 rank 2 etc.
If two entries have the same value they receive the same rank and the rank for a lower value is raised.
So for this array, the new ranking array would be:
[2, 7, 4, 7, 3, 9, 5, 9, 1, 6]
How can I do this?
var arr = [79, 5, 18, 5, 32, 1, 16, 1, 82, 13];
var sorted = arr.slice().sort(function(a,b){return b-a})
var ranks = arr.map(function(v){ return sorted.indexOf(v)+1 });
console.log(ranks);
Result :
[2, 7, 4, 7, 3, 9, 5, 9, 1, 6]
If you want to be compatible with old browsers, you may have to define a shim for indexOf and for map (note that if you want to do this very fast for very big arrays, you'd better use for loops and use an object as map instead of indexOf).
This won't work with older browsers because it uses ECMAScript 5 features, but it allows you to quickly and succinctly produce an array of rankings even for very large arrays. (It doesn't use indexOf which does a linear search and thus can be slow for large arrays.)
function cmp_rnum(a,b) {
// comparison function: reverse numeric order
return b-a;
}
function index_map(acc, item, index) {
// reduction function to produce a map of array items to their index
acc[item] = index;
return acc;
}
function ranks(v) {
var rankindex = v.slice().sort(cmp_rnum).reduceLeft(index_map, Object.create(null));
// reduceLeft() is used so the lowest rank wins if there are duplicates
// use reduce() if you want the highest rank
return v.map(function(item){ return rankindex[item]+1; });
}
Example output:
> ranks([79, 5, 18, 5, 32, 1, 16, 1, 82, 13]);
[2, 7, 4, 7, 3, 9, 5, 9, 1, 6]
function rank(arr, f) {
return arr
.map((x, i) => [x, i])
.sort((a, b) => f(a[0], b[0]))
.reduce((a, x, i, s) => (a[x[1]] =
i > 0 && f(s[i - 1][0], x[0]) === 0 ? a[s[i - 1][1]] : i + 1, a), []);
}
Usage:
rank([79, 5, 18, 5, 32, 1, 16, 1, 82, 13], (a, b) => b - a);
// [2, 7, 4, 7, 3, 9, 5, 9, 1, 6]
Looks a bit ugly, but it doesn't use indexOf() or an object/map, so not only does it run a little faster, but more importantly, it respects the meaning of "same rankedness" as defined by the comparison function. If one uses indexOf() or an object, "same rankedness" can only mean a === b or String(a) === String(b).
Alternatively, use findIndex():
function rank(arr, f) {
const sorted = arr.slice().sort(f)
return arr.map(x => sorted.findIndex(s => f(x, s) === 0) + 1)
}
JavaScript ES6 simple two lines solution.
var arrayRankTransform = arr => {
const sorted = [...arr].sort((a, b) => b - a);
return arr.map((x) => sorted.indexOf(x) + 1);
};
console.log(arrayRankTransform([79, 5, 18, 5, 32, 1, 16, 1, 82, 13]));
I am not good at Javascript but in PHP it can be done quite easily the following way. Somebody good at JavaScript can come up with the relevant code.
$marks = [79, 5, 18, 5, 32, 1, 16, 1, 82, 13];
public function getRank($marks) {
$rank = 1; $count = 0; $ranks = [];
//sort the marks in the descending order
arsort($marks,1);
foreach($marks as $mark) {
//check if this mark is already ranked
if(array_key_exists($mark, $ranks)) {
//increase the count to keep how many times each value is repeated
$count++;
//no need to give rank - as it is already given
} else {
$ranks[$mark] = $i+$j;
$i++;
}
return $ranks;
}
I needed the same piece of code for an operations scheduling script I was writing. I used objects and their properties/keys, which can have any value and can be accessed whenever needed. Also, as far as I read in some articles, the search of properties in objects can be faster than search in arrays.
The script below has three simple steps:
sort the values (ascending or descending doesn't matter for the rest of the script)
find the ranks and number of occurrences for each value
replace the given values with ranks using the data from step 2
Note! The below script will not output duplicate ranks, but instead increments ranks for duplicate values/elements.
function rankArrayElements( toBeRanked ) {
// STEP 1
var toBeRankedSorted = toBeRanked.slice().sort( function( a,b ) { return b-a; } ); // sort descending
//var toBeRankedSorted = toBeRanked.slice().sort( function( a,b ) { return a-b; } ); // sort ascending
var ranks = {}; // each value from the input array will become a key here and have a rank assigned
var ranksCount = {}; // each value from the input array will become a key here and will count number of same elements
// STEP 2
for (var i = 0; i < toBeRankedSorted.length; i++) { // here we populate ranks and ranksCount
var currentValue = toBeRankedSorted[ i ].toString();
if ( toBeRankedSorted[ i ] != toBeRankedSorted[ i-1 ] ) ranks[ currentValue ] = i; // if the current value is the same as the previous one, then do not overwrite the rank that was originally assigned (in this way each unique value will have the lowest rank)
if ( ranksCount[ currentValue ] == undefined ) ranksCount[ currentValue ] = 1; // if this is the first time we iterate this value, then set count to 1
else ranksCount[ currentValue ]++; // else increment by one
}
var ranked = [];
// STEP 3
for (var i = toBeRanked.length - 1; i >= 0; i--) { // we need to iterate backwards because ranksCount starts with maximum values and decreases
var currentValue = toBeRanked[i].toString();
ranksCount[ currentValue ]--;
if ( ranksCount[ currentValue ] < 0 ) { // a check just in case but in theory it should never fail
console.error( "Negative rank count has been found which means something went wrong :(" );
return false;
}
ranked[ i ] = ranks[ currentValue ]; // start with the lowest rank for that value...
ranked[ i ] += ranksCount[ currentValue ]; // ...and then add the remaining number of duplicate values
}
return ranked;}
I also needed to do something else for my script.
The above output has the following meaning:
index - the ID of the element in the input array
value - the rank of the element from the input array
And I needed to basically 'swap the index with the value', so that I have a list of element IDs, arranged in the order of their ranks:
function convertRanksToListOfElementIDs( ranked ) { // elements with lower ranks will be first in the list
var list = [];
for (var rank = 0; rank < ranked.length; rank++) { // for each rank...
var rankFound = false;
for (var elementID = 0; elementID < ranked.length; elementID++) { // ...iterate the array...
if ( ranked[ elementID ] == rank ) { // ...and find the rank
if ( rankFound ) console.error( "Duplicate ranks found, rank = " + rank + ", elementID = " + elementID );
list[ rank ] = elementID;
rankFound = true;
}
}
if ( !rankFound ) console.error( "No rank found in ranked, rank = " + rank );
}
return list;}
And some examples:
ToBeRanked:
[36, 33, 6, 26, 6, 9, 27, 26, 19, 9]
[12, 12, 19, 22, 13, 13, 7, 6, 13, 5]
[30, 23, 10, 26, 18, 17, 20, 23, 18, 10]
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
[7, 7, 7, 7, 7, 2, 2, 2, 2, 2]
[2, 2, 2, 2, 2, 7, 7, 7, 7, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
rankArrayElements( ToBeRanked ):
[0, 1, 8, 3, 9, 6, 2, 4, 5, 7]
[5, 6, 1, 0, 2, 3, 7, 8, 4, 9]
[0, 2, 8, 1, 5, 7, 4, 3, 6, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
convertRanksToListOfElementIDs( rankArrayElements( ToBeRanked ) ):
[0, 1, 6, 3, 7, 8, 5, 9, 2, 4]
[3, 2, 4, 5, 8, 0, 1, 6, 7, 9]
[0, 3, 1, 7, 6, 4, 8, 5, 2, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
IMHO several solutions here are incorrect as they do not correctly handle values that occur after repeated values. Such followers should get the next rank. The highest rank should equal the number of unique values in the array. This solution (in PHP) is, IMHO, correct. Basically #Suresh's solution with the bugs removed.
function rank($marks){
$rank = 1; $ranks = [];
rsort($marks,SORT_NUMERIC);
foreach($marks as $mark) {
if(!isset($ranks[$mark])) {
$ranks[$mark] = $rank++;
}
}
return $ranks;
}
This should work with duplicate keys in the array
function rank(arry) {
let sorted = arry.slice().sort(function (a, b) {
return b - a
});
let currentRank = sorted.length;
let rankValue = null;
let ranks = [];
sorted.forEach(value => {
if(value !== rankValue && rankValue !==null) {
currentRank--;
}
ranks.push({value,currentRank});
rankValue = value;
});
let mapRanksToArrayValues = arry.map(function (x) {
let _rank = null;
ranks.forEach( rank => {
if(rank.value === x ) {
_rank = rank.currentRank;
return;
}
});
return _rank;
});
return mapRanksToArrayValues;
}
I created Rank_JS Pro.
<script>https://cdn.statically.io/gl/maurygta2/mquery/master/Rank Tools/rank.js</script>
Basics Methods:
var a = {
b: 2,
c: 7
}
Rank_Tools.rank(a,(pos,name,value) => {
return pos + ". "+name+" "+value;
})
// result
// rank1 = 1. c 7
// rank 2 = 2. b 2
This alternate way doesn't require the input array to be sorted:
// O(n^2)
const rank = (arr) => {
// Create a temporary array to keep metadata
// regarding each entry of the original array
const tmpArr = arr.map(v => ({
value: v,
rank: 1,
}));
// Get rid of douplicate values
const unique = new Set(arr);
// Loops through the set
for (let a of unique) {
for (let b of tmpArr) {
// increment the order of an element if a larger element is pressent
if (b.value < a) {
b.rank += 1;
}
}
}
// Strip out the unnecessary metadata
return tmpArr.map(v => v.rank);
};
console.log(rank([2600, 200, 36, 36, 400, 2, 0, 0]));
// => [1, 3, 4, 4, 2, 5, 6, 6]
console.log(rank([79, 5, 18, 5, 32, 1, 16, 1, 82, 13]));
// => [2, 7, 4, 7, 3, 8, 5, 8, 1, 6]
I had the same homework and this one works well, also it's easier to understand if you are new to this.
function rankings(arr) {
let rankingsArr = [];
for (let i = 0; i < arr.length; i++) {
var rank = 1;
for (let j = 0; j < arr.length; j++) {
if (arr[j] > arr[i]) rank++;
}
rankingsArr.push(rank);
}
return rankingsArr;
}
In javascript I have an array as follows:
var foo = [2, 2, 4, 4, 128, 2, 2, 1, 4, 18, 27, 16, 2, 1, 18, 21, 5, 1, 128, 1, 2, 2, 1, 18, 12, 60, 2, 28, 1, 17, 2, 3, 4, 2, 2, 2, 1, 27, 2, 17, 7, 2, 2, 2, 5, 1, 2, 4, 7, 1, 2, 1, 1, 1, 2, 1, 5, 7, 2, 7, 6, 1, 7, 1, 5, 8, 4];
And I am interested in finding a way (within one loop, not multiple) to derive a subset array of the highest 10 values, where the previous position of the value is the 'key' (so simulating a Map object):
eg:
var fooTopTen = [[4, 128], [18, 128], [25, 60], [27, 28], [10, 27], [37, 27], [15, 21], [9, 18], [14, 18], [23, 18]];
My previous answer used a reverse index table, but contained some bugs - which are now fixed - and was harder to understand than the following code.
This is actually the slowest of all solutions given in the answers - for maximum performance, check my other answer.
var foo = [2, 2, 4, 4, 128, 2, 2, 1, 4, 18, 27, 16, 2, 1, 18, 21, 5, 1, 128, 1, 2, 2, 1, 18, 12, 60, 2, 28, 1, 17, 2, 3, 4, 2, 2, 2, 1, 27, 2, 17, 7, 2, 2, 2, 5, 1, 2, 4, 7, 1, 2, 1, 1, 1, 2, 1, 5, 7, 2, 7, 6, 1, 7, 1, 5, 8, 4];
var fooTopTen = [];
// add index to values
for(var i = 0, len = foo.length; i < len; ++i)
fooTopTen.push([i, foo[i]]);
// sort first by value (descending order), then by index (ascending order)
fooTopTen.sort(function(t1, t2) {
return t2[1] - t1[1] || t1[0] - t2[0];
});
// shorten array to correct size
fooTopTen.length = 10;
// output top ten to check result
document.writeln('[[' + fooTopTen.join('], [') + ']]');
The second part of the comparison function (the one comparing the indices) is not needed, as sort() is stable in most implementations (this isn't required by ECMA according to MDC). I'll leave it in as an example to how sorting with multiple requirements can be done...
This runs once through the main array it searches, inserting items at the appropriate place in the results array:
function top10(arr) {
var results = [[0,Number.MAX_VALUE],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]];
for (var i=0; i<arr.length; i++) {
// search from back to front
for (var j=9; j>=0; j--) {
if (arr[i] <= results[j][1]) {
if (j==9)
break;
results.splice(j+1, 0, [i, arr[i]]);
results.pop();
break;
}
}
}
return results.slice(1);
}
For large arrays this should even be rather fast, since most times the inner loop should only do one iteration.
Here's the de-bugged version of my previous answer using an index table. I did a little benchmarking and for the input given in the question, this solition will be faster than anything else which has been suggested in this thread till now:
var foo = [2, 2, 4, 4, 128, 2, 2, 1, 4, 18, 27, 16, 2, 1, 18, 21, 5, 1, 128, 1, 2, 2, 1, 18, 12, 60, 2, 28, 1, 17, 2, 3, 4, 2, 2, 2, 1, 27, 2, 17, 7, 2, 2, 2, 5, 1, 2, 4, 7, 1, 2, 1, 1, 1, 2, 1, 5, 7, 2, 7, 6, 1, 7, 1, 5, 8, 4];
var indexTable = {}, uniqueValues = [];
// --- build reverse index table, find unique values
for(var i = foo.length; i--; ) {
var value = foo[i];
if(indexTable.hasOwnProperty(value))
indexTable[value].push(i);
else {
indexTable[value] = [i];
uniqueValues.push(value);
}
}
// --- sort unique values in ascending order
uniqueValues.sort(function(i1, i2) {
return i1 - i2;
});
// --- find ten greatest values
var fooTopTen = [], k = 0;
for(var i = uniqueValues.length; k < 10 && i--; ) {
var value = uniqueValues[i],
indices = indexTable[value];
for(var j = indices.length; k < 10 && j--; )
fooTopTen[k++] = [indices[j], value];
}
// --- output result
document.writeln('[[' + fooTopTen.join('], [') + ']]');
var foo = [2, 2, 4, 4, 128, 2, 2, 1, 4, 18, 27, 16, 2, 1, 18, 21, 5, 1, 128, 1, 2, 2, 1, 18, 12, 60, 2, 28, 1, 17, 2, 3, 4, 2, 2, 2, 1, 27, 2, 17, 7, 2, 2, 2, 5, 1, 2, 4, 7, 1, 2, 1, 1, 1, 2, 1, 5, 7, 2, 7, 6, 1, 7, 1, 5, 8, 4];
var index = 0;
var result = foo.map( function(a){ return [index++, a]; } )
.sort( function(a,b){ return (a[1] < b[1]); } )
.splice( 0, 10 );
document.write(result.join( ' ' ));
If foo is very large compared to the size of result required, it may be quicker to iterate over foo insertion-sorting each element into result as we come across it.
// Sorting method
function sortNumber(a, b) {
return a - b;
}
// Find the offset of an element in array
function findOffset(element, array) {
for (var i = 0; i < array.length; i++) {
if (array[i] == element) {
// Make sure we don't find it again
array[i] = null;
return i;
}
}
}
var foo = [2, 2, 4, 4, 128, 2, 2, 1, 4, 18, 27, 16, 2, 1, 18, 21, 5, 1, 128, 1, 2, 2, 1, 18, 12, 60, 2, 28, 1, 17, 2, 3, 4, 2, 2, 2, 1, 27, 2, 17, 7, 2, 2, 2, 5, 1, 2, 4, 7, 1, 2, 1, 1, 1, 2, 1, 5, 7, 2, 7, 6, 1, 7, 1, 5, 8, 4];
// Copies
var bar = foo.slice();
var baz = foo.slice();
var fooTopTen = new Array(10);
// Sort
bar.sort(sortNumber).reverse();
// Create the results
for (var i = 0; i < 10; i++) {
fooTopTen[i] = new Array(2);
fooTopTen[i][0] = findOffset(bar[i], baz);
fooTopTen[i][1] = bar[i];
}
computer-science-y answer:
problem statement: Given a large array X of length N, and a small number m < N (here m=10), produce an array Y of length m where each element of Y contains the pair {i,X[i]} such that the set of X{i} are the m largest elements of X.
If m is much smaller than N, then loop over elements of X and sort them into Y, discarding pairs to maintain at most m elements. (i.e. as moonshadow mentioned)
Sorting X will cost you O(N log N) elements. Iterating over X and sorting into Y should cost you only O(N log m) elements.