मान लीजिए हमारे पास लिटरल्स के दो ऐरे हैं:
const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6];
हमें एक जावास्क्रिप्ट फंक्शन लिखना है जो ऐसे दो ऐरे ले और एक नया ऐरे वापस करे जिसमें सभी डुप्लिकेट हटाए गए हों (केवल एक बार आने चाहिए)।
उदाहरण
इसके लिए कोड:
const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; const mergeArrays = (first, second) => { const { length: l1 } = first; const { length: l2 } = second; const res = []; let temp = 0; for(let i = 0; i < l1+l2; i++){ if(i >= l1){ temp = i - l1; if(!res.includes(first[temp])){ res.push(first[temp]); }; }else{ temp = i; if(!res.includes(second[temp])){ res.push(second[temp]); }; }; }; return res; }; console.log(mergeArrays(arr1, arr2));आउटपुट
कंसोल में आउटपुट:
[ 1, 4, 5, 2, 3, 7, 6, 8, 9 ]