let firstArr = ["a","a","b","c","d"]
let secondArr = ["z","x","y","f","bd"]
//find whether both arr has any comon values or not
function findCommon(firstArr ,secondArr){
for(let i = 0 ;i < firstArr.length ;i++){
for(let j = 0; j <secondArr.length ; j++){
if(firstArr[i] === secondArr[j] ){
return true
}
}
}
return false
}
console.log(findCommon(firstArr,secondArr)) // THIS APPROCH HAS O(n*m) time complaxity and O(1) space complaxity and this is brute force solution of the problem
/*
Now here another thing to learn that in whenever we have multiple nested loop kind of solution then first thing we must do is convert first array into
object. and then compare those key into next array
*/
function findCommonTwo(firstArr,secondArr){
// convert first array into object
// loop through second object and compare it with objects
let obj = {}
for(let i = 0 ; i <firstArr.length ; i++){
obj[firstArr[i]] = true
}
for(let i = 0 ; i < secondArr.length ; i++){
if(obj[secondArr[i]]) return true
}
return false
}
let output =findCommonTwo(firstArr,secondArr)
console.log(output)
//Now in second function we have easily see one bennifit like we don't have nested loops we have loops but both are seperated loops so time complaxity
//of this will be O(n+m) why n+m because we have two different inputs and why + because loops are not nested they are one after another
// Now this is i never thought but we can do same thing using array inbuild method
function findCommonWithInbuiltMethod(){
let isPresent = firstArr.some(item => secondArr.includes(item))
console.log(isPresent)
}
findCommonWithInbuiltMethod();
// google find pair with some solution
let arr = [1,2,4,5]
let sum = 9
// Best example if you understand it well
function findPairSum(arr,sum){
let map = new Set()
for(let i = 0 ; i <arr.length ; i++){
if(map.has(arr[i])){
return true
}
map.add(sum -arr[i])
}
return false
}
console.log(findPairSum(arr,sum))