উপাদান সরাতে, আমাদের সেগুলো খুঁজে বের করতে হবে এবং একটি সাধারণ splice ফাংশন কল ব্যবহার করে সেটি অ্যারের থেকে במקוםে সরাতে হবে।
আসুন এর বাস্তবায়ন দেখি:
উদাহরণ
remove(key) {
let hashCode = this.hash(key);
for (let i = 0; i < this.container[hashCode].length; i++) {
// চেইন থেকে উপাদানটি খুঁজুন
if (this.container[hashCode][i].key === key) {
this.container[hashCode].splice(i, 1);
return true;
}
}
return false;
}
আপনি নিচের কোড দিয়ে এটি পরীক্ষা করতে পারেন:
উদাহরণ
let ht = new HashTable(); ht.put(10, 94); ht.put(20, 72); ht.put(30, 1); ht.put(21, 6); ht.put(15, 21); ht.put(32, 34); console.log(ht.get(20)); console.log(ht.remove(20)); console.log(ht.get(20)); console.log(ht.remove(20));
আউটপুট
এটি নিচের আউটপুট দেবে:
{ key: 20, value: 72 }
true
undefined
false
প্রথমবার এটি true ফেরত দিয়েছে কারণ এটি পাওয়া গিয়েছিল এবং সফলভাবে মুছে ফেলা হয়েছিল। দ্বিতীয়বার, যেহেতু এটি উপস্থিত ছিল না, remove ফাংশনটি false ফেরত দিয়েছে।