ধরুন, আমাদের দুটি সংখ্যার অ্যারে আছে −
const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];
আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা দুটি এমন অ্যারে নেবে এবং যেগুলো দুটোতে সাধারণ নয় সেই এলিমেন্টগুলো রিটার্ন করবে।
উদাহরণ
নিচের কোডটি দেখুন −
const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; const unCommonArray = (first, second) => { const res = []; for(let i = 0; i < first.length; i++){ if(second.indexOf(first[i]) === -1){ res.push(first[i]); } }; for(let j = 0; j < second.length; j++){ if(first.indexOf(second[j]) === -1){ res.push(second[j]); }; }; return res; }; console.log(unCommonArray(arr1, arr2));
আউটপুট
কনসোলে নিচের আউটপুট দেখাবে −
[ 6, 5, 1 ]