-1

I'm curious if there is a good way to find the first occurrence of element from array in another array. As result, the answer should be a null (if no elements from the first array in the second array) or this element. Ex.1:

fromArr = ["Yellow", "Green", "Orange", "Apple", "Banana", "Apple"];
whereArr= ["Apple", "Orange", "Banana", "Apple"];

//the answer should be "Orange", as the order counts (first we check for "Yellow", then for "Green", then for "Orange" - occurrence found!).

Ex.2:

fromArr = ["Orange", "Apple", "Banana", "Apple"];
whereArr= ["November", "December", "April", "Banana"];

//the answer should be "Banana".

Ex.3:

fromArr = ["Orange", "Apple"];
whereArr= ["November", "December", "April"];

//the answer should be null.

Thank you

ford04
  • 66,267
  • 20
  • 199
  • 171
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212
  • Do you need `null` or will `undefined` be fine if no item can be found? – Nick Parsons Sep 18 '20 at 08:38
  • Am I missing something? `fromArr.forEach(item => console.log(whereArr.find(item2 => item === item2)))` –  Sep 18 '20 at 08:38
  • 1
    Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted. – Nick Sep 25 '20 at 05:23

3 Answers3

2

You can do this easily by using "find" on the "fromArr" Array and compare if an entry is included within the "whereArr" Array.

Example:

fromArr = ["Yellow", "Green", "Orange", "Apple", "Banana", "Apple"];
whereArr = ["Apple", "Orange", "Banana", "Apple"];
fromArr.find(e => whereArr.includes(e));
2

You can use .find() on your fromArr, and convert your whereArr to a Set to allow for efficient (sublinear) lookup. .find() will loop through all your values in fromArr until the callback returns true. Once the callback returns true, .find() will return the value you're currently iterated on. You can check if the set has the value by using .has(), which will return true when a value is within the set. If find's callback doesn't return true for any values inside your fromArr, it will result in undefined.

See example below:

const fromArr = ["Yellow", "Green", "Orange", "Apple", "Banana", "Apple"];
const whereSet = new Set(["Apple", "Orange", "Banana", "Apple"]);

const result = fromArr.find(val => whereSet.has(val));
console.log(result);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
1

You can make an object with the index number of the items of fromArr. Then check the every element of whereArr array and which item has the smallest index(from the hash) value this is the first occurence. This method takes O(n+m) time.

function getFirstOccurance(fromArr, whereArr) {
  const indics = fromArr.reduce((acc, curr, i) => ({...acc, [curr]: i}), {});

  let min = Number.MAX_SAFE_INTEGER;
  let res = null;
  whereArr.forEach(item => {
    if (indics[item] < min) {
      min = indics[item];
      res = item;
    }
  });

  return res;
}

let arr = ["Yellow", "Green", "Orange", "Apple", "Banana", "Apple"];
let arr2= ["Apple", "Orange", "Banana", "Apple"];
console.log(getFirstOccurance(arr, arr2));

arr = ["Orange", "Apple", "Banana", "Apple"];
arr2 = ["November", "December", "April", "Banana"];
console.log(getFirstOccurance(arr, arr2));

arr = ["Orange", "Apple"];
arr2= ["November", "December", "April"];
console.log(getFirstOccurance(arr, arr2));
.as-console-wrapper{min-height: 100%; top: 0}
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30