如何编写一个程序,打印同时出现在两个数组中的最大值?

huangapple go评论67阅读模式
英文:

how to write a program that prints the largest value that occurs simultaneously in both arrays?

问题

我必须创建两个包含1到20之间随机值的10元素数组,并编写一个程序,打印同时出现在这两个数组中的最大值。

我创建了两个如下所示的数组。程序应该打印同时出现在这两个数组中的最大值。这里应该是11。我只知道如何从数组中获取最大值。感谢帮助。

<script>
var max = 0;
var tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
var tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
for (var i = 0; i < tab.length; i++) {
if (max <= tab[i]) {
max = tab[i];
}
}
console.log(max);
</script>
英文:

I have to create two 10-elements arrays with random values from 1 to 20 and write a program that prints the largest value that occurs simultaneously in both arrays.

I created two tabs like below. The program should prints the largest value that occurs simultaneously in both arrays. Here it should be 11. I know just how to catch the max value from the array. I appreciate help.

&lt;script&gt;
var max = 0;
var tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
var tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
for (var i = 0; i &lt; tab.length; i++) {
if (max &lt;= tab[i]) {
max = tab[i];
}
}
console.log(max);
&lt;/script&gt;

答案1

得分: 1

使用嵌套循环来查找最大值,比较两个数组的每个元素如下所示:

var tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
var tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
var max = 0;
for (var i = 0; i < tab.length; i++) {
  for (var j = 0; j < tab2.length; j++) {
    if (tab[i] === tab2[j] && tab[i] > max) {
      max = tab[i];
    }
  }
}
console.log(max);
英文:

to find the largest value use nested loops to compare each element of both arrays as follow

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
var tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];
var max = 0;
for (var i = 0; i &lt; tab.length; i++) {
  for (var j = 0; j &lt; tab2.length; j++) {
    if (tab[i] === tab2[j] &amp;&amp; tab[i] &gt; max) {
      max = tab[i];
    }
  }
}
console.log(max);

<!-- end snippet -->

答案2

得分: 0

Sure, here's the translated JavaScript code:

// const {intersection,max} require('lodash/fp'}
const { intersection, max } = _;

const tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
const tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];

const res = max(intersection(tab, tab2))

console.log({
  intersection: intersection(tab, tab2),
  max: max(intersection(tab, tab2)),
});

The HTML part does not need translation as it contains only HTML tags and a link to the lodash library.

英文:

I'd do this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// const {intersection,max} require(&#39;lodash/fp&#39;}
const { intersection, max } = _;

const tab = [1, 2, 5, 8, 9, 11, 15, 16, 17, 20];
const tab2 = [3, 4, 6, 7, 10, 11, 12, 13, 14, 18];

const res = max(intersection(tab, tab2))

console.log({
  intersection: intersection(tab, tab2),
  max: max(intersection(tab, tab2)),
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

You can translate that into straight javascript, especially if it's for a homework assignment :).

答案3

得分: 0

以下是翻译好的内容:

... 然后要么自己实现一个交集功能,要么使用第三方库/辅助功能。

下面的示例代码包含了getIntersection的一个可能实现,该实现使用...

function getIntersection(firstIterable = [], secondIterable = []) {
  const [
    comparisonBase, // ... 与较短的数组进行比较。
    comparisonList, // ... 从较长的数组中筛选。
  ] = [Array.from(firstIterable), Array from(secondIterable)]
    // - 确保两个数组(上一行)
    // - 并根据每个数组的`length`对它们进行排序。
    .sort((a, b) => a.length - b.length);

  // 从较短的数组创建一个基于`Set`的查找。
  const itemLookup = new Set(comparisonBase);

  // 交集是以下过滤任务的结果。
  return comparisonList.filter(item => itemLookup.has(item));
}

const tab = [1, 2, 4, 5, 8, 9, 11, 15, 16, 17, 20, 21, 0];
const tab2 = [3, 4, 6, 7, 9, 10, 11, 12, 13, 14, 18, 19, 0];

console.log(
  'tab`和`tab2`的交集...',
  getIntersection(tab, tab2)
);
console.log(
  'tab`和`tab2`的交集的最大值...',
  Math.max(...getIntersection(tab, tab2))
);
英文:

The approach is as follows ...

  • get the intersection of both arrays

  • get the maximum value of the just computed intersection

... which then boils down either to coming up with an own implementation of an intersection functionality or to making use of a third party library / helper functionality.

The below example code features one possible implementation of getIntersection which makes use of ...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function getIntersection(firstIterable = [], secondIterable = []) {
  const [

    comparisonBase, // ... compare to the shorter array.
    comparisonList, // ... filter from the longer array.

  ] = [Array.from(firstIterable), Array.from(secondIterable)]
    // - ensure two arrays (line above)
    // - and sort them according to each array&#39;s `length`.
    .sort((a, b) =&gt; a.length - b.length);

  // create a `Set` based lookup from the shorter array.
  const itemLookup = new Set(comparisonBase);

  // the intersection is the result of following filter task.
  return comparisonList.filter(item =&gt; itemLookup.has(item));
}

const tab = [1, 2, 4, 5, 8, 9, 11, 15, 16, 17, 20, 21, 0];
const tab2 = [3, 4, 6, 7, 9, 10, 11, 12, 13, 14, 18, 19, 0];

console.log(
  &#39;intersection of `tab` and `tab2` ...&#39;,
  getIntersection(tab, tab2)
);
console.log(
  &#39;max value of the intersection of `tab` and `tab2` ...&#39;,
  Math.max(...getIntersection(tab, tab2))
);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月7日 03:36:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定