猿问

我已经在hackerrank中完成了这个代码,排名第二。

javscript 第二大没有。代码请帮我解决这个程序


function getSecondLargest(nums) {

    // Complete the function

    nums.sort();

    //console.log("sorted array", nums);

    let largest_no = nums[nums.length-1];

    let final_array = nums.filter(x => x != largest_no);

    let second_largest_no = final_array[final_array.length-1]

    console.log("filtered array", final_array);

    return second_largest_no;

}

getSecondLargest([2, 2, 1, 2, 5,  1, 5, 3, 4, 6, 6 , 6 , 5 , 5]);


MYYA
浏览 139回答 2
2回答

qq_笑_17

尝试使用reduce具有O(N)复杂性的方法。所以我们可以使用reducemethod来遍历数组,并获得诸如方法largeValue: 0, largerValue: 0, largestValue: 0累加器之类的值reduce:const obj = arr.reduce((a, c, i) => {&nbsp; &nbsp;a[c] = a[c] || {c};&nbsp; &nbsp;if (i === 0) {&nbsp; &nbsp; &nbsp; a.largeValue = c;&nbsp; &nbsp; &nbsp; a.largerValue = c;&nbsp; &nbsp; &nbsp; a.largestValue = c;&nbsp; &nbsp;}&nbsp; &nbsp;else {&nbsp; &nbsp; &nbsp; if (c > a.largeValue && c < a.largerValue && c > a.largestValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.largeValue = c;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else if (c > a.largeValue && c > a.largerValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (a.largerValue == a.largestValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.largerValue = c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.largestValue = c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;return a;},{ largeValue: 0, largerValue: 0, largestValue: 0 });一个例子:let arr = [2, 2, 1, 2, 5,&nbsp; 1, 5, 3, 4, 6, 6 , 6 , 5 , 5];const obj = arr.reduce((a, c, i) => {&nbsp; &nbsp;a[c] = a[c] || {c};&nbsp; &nbsp;if (i === 0) {&nbsp; &nbsp; &nbsp; a.largeValue = c;&nbsp; &nbsp; &nbsp; a.largerValue = c;&nbsp; &nbsp; &nbsp; a.largestValue = c;&nbsp; &nbsp;}&nbsp; &nbsp;else {&nbsp; &nbsp; &nbsp; if (c > a.largeValue && c < a.largerValue && c > a.largestValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.largeValue = c;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else if (c > a.largeValue && c > a.largerValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (a.largerValue == a.largestValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.largerValue = c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.largestValue = c;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;return a;},{ largeValue: 0, largerValue: 0, largestValue: 0 });console.log(obj.largerValue);

富国沪深

如果他们通过忽略重复来寻找“5”作为第二大数字。然后下面的代码可用于查找第二个最大值。可以根据您的要求设置“Default_Min”。该算法将具有 O(n) 时间复杂度。这比使用排序和过滤要好。function SencondLargest(nums){&nbsp; &nbsp; let max_1;&nbsp; &nbsp; let max_2;&nbsp; &nbsp; let Default_Min = 0;&nbsp; &nbsp; if (nums.length < 2)&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; console.log(" Invalid Input ");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; max_1 = max_2 = Default_Min;&nbsp; &nbsp; for(let i=0; i<nums.length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (nums[i] > max_1)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_2 = max_1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_1 = nums[i];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else if (nums[i] > max_2 && nums[i] != max_1)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_2 = nums[i];&nbsp; &nbsp; }&nbsp; &nbsp; if (max_2 == Default_Min)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; console.log("There is no second largest"+ " element\n");&nbsp;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; console.log("The second largest element"+ " is "+ max_2);&nbsp;}var x = [2, 2, 1, 2, 5,&nbsp; 1, 5, 3, 4, 6, 6 , 6 , 5 , 5];SencondLargest(x);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答