# 我如何与数组中的其余元素进行比较

2023-01-06 15:38:00

Input: nums = [8,1,2,2,3]

Output: [4,0,1,1,3]

Explanation:

For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).

For nums[1]=1 does not exist any smaller number than it.

For nums[2]=2 there exist one smaller number than it (1).

For nums[3]=2 there exist one smaller number than it (1).

For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

var smallerNumbersThanCurrent = (nums) => {

const output = []

const max = nums.reduce(function(a, b) {

return Math.max(a, b);

});

const min = nums.reduce(function(a, b) {

return Math.min(a, b);

});

for(let i = 0; i < nums.length; i++){

if(nums[i] === max){

output.push(nums.length - 1)

} else if (nums[i] === min){

output.push(0)

}

else if (nums[i] < max && nums[i] > min){

//how do i compare with rest of the elements in the array?

}

}

}

## 5 回答

TA贡献1548条经验 获得超5个赞

nums = [8,1,2,2,3];

for (let i = 0; i < nums.length; i++) {

let count = 0;

for (let j = 0; j < nums.length; j++) {

if (nums[j] < nums[i]) {

count++;

}

}

console.log(`For nums[\${i}]=\${nums[i]} there are \${count} lower numbers`);

}

TA贡献1736条经验 获得超6个赞

const nums = [8,1,2,2,3]

const sorted = [...nums].sort();

const result = nums.map((i) => {

return sorted.findIndex(s => s === i);

});

console.log(result);

TA贡献1516条经验 获得超10个赞

const nums = [8,1,2,2,3];

const smallerNums = nums.map(v => nums.filter(n => n < v).length);

console.log(smallerNums); // [4,0,1,1,3]

const nums = [8, 1, 2, 2, 3];

const smallerNums = nums.map(v => nums.reduce((c, n) => c += (n < v), 0));

console.log(smallerNums); // [4,0,1,1,3]

TA贡献1590条经验 获得超3个赞

function rankZero(array){

const s = [...array], r = [];

s.sort((a, b)=>{

return a - b;

});

for(let n of array){

r.push(s.indexOf(n));

}

return r;

}

console.log(rankZero([8, 1, 2, 2, 3]));

TA贡献1576条经验 获得超11个赞

const datalength = 1000;

const iterations = 100;

const getRandom = (min, max) => Math.random() * (max - min) + min;

const data = Array.from({

length: datalength

}, () => getRandom(1, 100));

const mapper = arr => arr.map(i => arr.filter(n => n < i).length);

const sorter = nums => {

const sorted = [...nums].sort();

const result = nums.map((i) => {

return sorted.findIndex(s => s === i);

});

};

const iterator = arr => {

for (let i = 0; i < arr.length; i++) {

let count = 0;

for (let j = 0; j < arr.length; j++) {

if (arr[j] < arr[i]) {

count++;

}

}

}

};

const rankZero = array => {

const s = [...array],

r = [];

s.sort((a, b) => {

return a - b;

});

for (let n of array) {

r.push(s.indexOf(n));

}

return r;

}

const reducer = arr => arr.map(v => arr.reduce((c, n) => c += (n < v), 0));

let fns = {

'iterator': iterator,

'mapper': mapper,

'sorter': sorter,

'reducer': reducer,

'rankZero': rankZero

}

for (let [name, fn] of Object.entries(fns)) {

let total = 0;

for (i = 0; i < iterations; i++) {

let t0 = performance.now();

fn(data);

let t1 = performance.now();

total += t1 - t0;

}

console.log(name, (total / iterations).toFixed(2));

}

• 5 回答
• 0 关注
• 16 浏览

0/150