हमें एक JavaScript फ़ंक्शन लिखना आवश्यक है जो लिटरल मानों की एक सरणी लेता है।
सरणी में कुछ दोहराव वाले मान हो सकते हैं।
हमारा फ़ंक्शन सरणी से उन सभी मानों को हटा देना चाहिए जो दोहरा रहे हैं। हमें ऐसे सभी तत्वों के सभी उदाहरणों को हटाना आवश्यक है।
उदाहरण
इसके लिए कोड होगा −
const arr = [1, 2, 3, 2, 4]; const removeAllInstances = (arr = []) => { filtered = arr.filter(val => { const lastIndex = arr.lastIndexOf(val); const firstIndex = arr.indexOf(val); return lastIndex === firstIndex; }); return filtered; }; console.log(removeAllInstances(arr));आउटपुट
और कंसोल में आउटपुट होगा −
[ 1, 3, 4 ]