从数组中查找元素在另一个数组中的第一次出现
Find the first occurrence of element from array in another array
我很好奇是否有一种好方法可以在另一个数组中找到数组中元素的第一次出现。
结果,答案应该是 null(如果第二个数组中的第一个数组中没有元素)或这个元素。
例 1:
fromArr = ["Yellow", "Green", "Orange", "Apple", "Banana", "Apple"];
whereArr= ["Apple", "Orange", "Banana", "Apple"];
//答案应该是“橙色”,因为顺序很重要(首先我们检查“黄色”,然后检查“绿色”,然后检查“橙色” - 找到了!)。
示例 2:
fromArr = ["Orange", "Apple", "Banana", "Apple"];
whereArr= ["November", "December", "April", "Banana"];
//答案应该是“香蕉”。
示例 3:
fromArr = ["Orange", "Apple"];
whereArr= ["November", "December", "April"];
//答案应该为空。
谢谢
您可以通过在“fromArr”数组上使用“查找”并比较“whereArr”数组中是否包含条目来轻松完成此操作。
示例:
fromArr = ["Yellow", "Green", "Orange", "Apple", "Banana", "Apple"];
whereArr = ["Apple", "Orange", "Banana", "Apple"];
fromArr.find(e => whereArr.includes(e));
您可以使用 .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()
,当值在集合内时,它将 return 为真。如果 find 的回调对于 fromArr 中的任何值都不 return 为真,它将导致 undefined
.
参见下面的示例:
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);
您可以用fromArr
项的索引号创建一个对象。然后检查 whereArr
数组的每个元素以及哪个项目具有最小的索引(来自哈希)值这是第一次出现。此方法需要 O(n+m)
时间。
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}
我很好奇是否有一种好方法可以在另一个数组中找到数组中元素的第一次出现。 结果,答案应该是 null(如果第二个数组中的第一个数组中没有元素)或这个元素。 例 1:
fromArr = ["Yellow", "Green", "Orange", "Apple", "Banana", "Apple"];
whereArr= ["Apple", "Orange", "Banana", "Apple"];
//答案应该是“橙色”,因为顺序很重要(首先我们检查“黄色”,然后检查“绿色”,然后检查“橙色” - 找到了!)。
示例 2:
fromArr = ["Orange", "Apple", "Banana", "Apple"];
whereArr= ["November", "December", "April", "Banana"];
//答案应该是“香蕉”。
示例 3:
fromArr = ["Orange", "Apple"];
whereArr= ["November", "December", "April"];
//答案应该为空。
谢谢
您可以通过在“fromArr”数组上使用“查找”并比较“whereArr”数组中是否包含条目来轻松完成此操作。
示例:
fromArr = ["Yellow", "Green", "Orange", "Apple", "Banana", "Apple"];
whereArr = ["Apple", "Orange", "Banana", "Apple"];
fromArr.find(e => whereArr.includes(e));
您可以使用 .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()
,当值在集合内时,它将 return 为真。如果 find 的回调对于 fromArr 中的任何值都不 return 为真,它将导致 undefined
.
参见下面的示例:
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);
您可以用fromArr
项的索引号创建一个对象。然后检查 whereArr
数组的每个元素以及哪个项目具有最小的索引(来自哈希)值这是第一次出现。此方法需要 O(n+m)
时间。
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}