🔴 Important JavaScript Interview #12: Program to Compare two Arrays are Equal or Not in JavaScript

preview_player
Показать описание
Welcome, 🔴 Important JavaScript Interview #12: Program to Compare two Arrays are Equal or Not in JavaScript

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

************* Must Watch Videos For Web Development *************



😍😍 Check Programming Videos in One Hour👇

*********** CLICK HERE TO WATCH *************

***************** MUST WATCH VIDEOS ******************

Make Website Responsive Using Media Queries in One Video in Hindi | Web Design Tutorial in Hindi

Guys, Please support my channel by SUBSCRIBE to my channel and share my videos in your Social Network TimeLines.

Don't Forget to Follow me on all Social Network,

Рекомендации по теме
Комментарии
Автор

arr1 = [1, 1, 2],
arr2 = [1, 2, 2]

these two are different arrays but your code shows them equal

jpwebdev
Автор

Sir make a ecommerce website with nodejs express and mongodb fetch products details, img, etc. We really need this type of contents

aweshkhan
Автор

function compareArray(arr1, arr2) {

if (arr1.length != arr2.length)
{
return false
}
else {
for ( let i=0; i <arr1.length; i++){
if(arr1[i] != arr2[i]){
return false
}
}
return true;
}

}

let arr1 = [4, 8, 10, 9, 5];
let arr2 = [5, 10, 8, 9, 4];


let result = compareArray(arr1, arr2);
if (result) {
console.log("both are equal");
} else {
console.log("both are not equal");
}

chandreshkumar
Автор

apke tutorial se bahut kuch sikhne ko milta hai thanku so much sir 😀😀

priyakarn
Автор

core coding without any pre defined function =>

const main = (arr, arr2) =>{
const map ={};
const map2 = {}

for( item of arr ){
map[item] = (map[item]||0)+1
}
for( item of arr2 ){
map2[item] = (map2[item]||0)+1
}


console.log(map);
console.log(map2);

for( key in map){

if( map[item]!== map2[item] ){
return false
}else{
return true
}

}

}


console.log( main([4, 8, 10, 9, 5], [5, 10, 8, 9, 4]));

mohit
Автор

we can do this way as well
for example :-
const array1 =[2, 6, 9, 3, 1];
const array2 =[3, 6, 1, 9, 2];

const arrayComparison = array1.length == array2.length &&
console.log(arrayComparison);

ujjwalsrivastava
Автор

Sir kindly yeh bata dein k JavaScript seekhnay k baad konsi pehlay karon aur phir pattern wise kia karon MERN Stack mein se
1. MongoDB
2. Express.js
3. React.js
4. Node.js

muhammadsalmanalam
Автор

what if we sort both array and convert them into string and then compare both strings?🤔

maulik
Автор

const isSame = (arr1, arr2)=>{
return a1.length === a2.length &&
a1.every( cV => a2.indexOf(cV) >-1 )
}

👉Try this one for compare two array✌✌

Tima-qlon
Автор

You can also compare two arrays using JSON.stringify().

JSON.stringify(array1) == JSON.stringify(array2)

praveenkumar
Автор

After completing this series

Vinod Sir😀
Please!!🙏🙏
Upload the CRUD operations in Firebase with React.

Vinod sir there is many youtuber but no one can explain than you .

Because you are Awesome ❤️❤️

NiteshYadav-dglh
Автор

let arr = [2, 4, 6, 8];
let arr2=[8, 6, 4, 2]

let isSame = arr.length===arr2.length && arr.every((value)=>{

return value
}
})
console.log(isSame)
//Output will be true

adityajaiswar
Автор

One of the best tutor I found in my life for learning programming concepts thanks alot sir.... Bcoz of u today I knew everything abt basics of javascript ❤

djayrok
Автор

Instead of indedexOf () method we can use includes () method.
btw the thanks sir.

adityajaiswar
Автор

Why you've used indexOf and why not includes() ?

siddiquiaffan
Автор

Just put array1 =[4, 4, 8, 10, 9]
In the same code and u will know this code is a joke.

AnilSingh-ultc
Автор

i think this is complicated, we should runa a loop to get each value from first array and a inner loop to check the value if its present in the second loop or not simple

benkkstudio
Автор

Bhaiya iska time complexity O(n^2) ata hai i guess. we can do array objects implentation with O(n).

mdmustaqahmed
Автор

Here you should compare the value (===) but in ur case you are assigning (=) but why its returning true am not getting

vamshi_travelling_life
Автор

I think below is the refactored code. I don't think inner indexOf() is required. Correct me if I am wrong.

var arr1 = [10, 11, 12, 13];
var arr2 = [13, 12, 11, 10];

const is_len_same = arr1.length == arr2.length && arr1.every((curr_elem) => {
if(arr2.indexOf(curr_elem) > -1) {
return true;
}
})

console.log(is_len_same);

LifeAtMH