Say i have a 2d array and i want to and i want to search for the occurence of the value int 3 I tried flattening the array and i'm assuming the time complexity is O(parentArraySize * subArraysSize) is there any way i can make it better i don't care about space, are there any algorithm or data strutures i could use Note: sub array size is not constant it can be bigger or smaller than 3
let twoDArray = [[1, 2, 3], [3, 5, 7], [3, 9, 10]];
let flattenArray = [];
for(let subAray of twoDArray){
for(let num of subAray){
flattenArray.push(num)
}
}
I've also tried implementing a binary search for the target value but it's only efficient for look for a single value not all possible targets
Instead of adding each value individually, you could concatenate each nested array which should make it slighly quicker.
let twoDArray = [[1, 2, 3], [3, 5, 7], [3, 9, 10]];
let flattenedArray = [];
for(let subAray of twoDArray){
flattenedArray = flattenedArray.concat(subAray)
}