1

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.

<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>
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
katarina80
  • 33
  • 3
  • 1
    Where exactly are you stuck on this? – mykaf Feb 06 '23 at 19:41
  • sort them both then loop through comparing a === b and see if you get a match. stop when you have one. – chovy Feb 06 '23 at 19:42
  • 1
    Break it down into two tasks, first, How would you find if an element is in both arrays? Then, if it is in both, what's the largest? The hard part is the first. To do this, you have to compare each element of array A with each other element in array B. The straightforward approach would be using two nested loops. – j.i.h. Feb 06 '23 at 19:43
  • Think about what `tab.filter(item => tab2.includes(item))` gets you, then take it from there. – Wyck Feb 06 '23 at 19:45
  • To continue on what @j.i.h. suggested - take a look at [this](https://stackoverflow.com/a/1885569/6133426). Combining `array.filter` and `include` will let you check if which of the elements of array A exist in array B, and going from there, you would use some of the ways suggested in this [SO entry](https://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array) to find out which of the elements is the largest one (check the answers, and read the comments to find the downsides). – FiddlingAway Feb 06 '23 at 19:59
  • "how to write a program" - open your favorite text editor and start typing. Do you have any **specific** question about this? – Nico Haase Feb 07 '23 at 08:41

3 Answers3

1

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

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);
Rory
  • 437
  • 1
  • 13
0

I'd do this:

// 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)),
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

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

Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
0

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 ...

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's `length`.
    .sort((a, b) => 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 => 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(
  'intersection of `tab` and `tab2` ...',
  getIntersection(tab, tab2)
);
console.log(
  'max value of the intersection of `tab` and `tab2` ...',
  Math.max(...getIntersection(tab, tab2))
);
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • Just curious; why use Map and not Set? Also, why wouldn't it be better to filter on the shorter array? – Ben Stephens Feb 06 '23 at 21:39
  • @BenStephens ... you're right, a `Set` instance might be the better structure for a fast lookup. And regarding which array to filter from and which to turn into a set/map, I remember it being the result of having run some tests with huge arrays. – Peter Seliger Feb 07 '23 at 08:42
  • Thanks, @PeterSeliger having done a quick and dirty test on a couple of not so large arrays, you appear to be correct. Given that Set lookups are supposed to be O(1) and filter would be O(n) I'm curious as to why that would be true. – Ben Stephens Feb 07 '23 at 09:07