Check if an element exists in an array, based on that update an array

Problem description :

Write a function which detects if the given element exists in an array or not.

  • If it does not exist then adds it to an array
  • If it does exist then logs the message saying that it already exists to an array

Input : An array // [‘mango’, ‘banana’, ‘grapes’]

Logic :

  • Find if the given element exist in an array or not using Array.prototype.indexOf()
  • If it does not exist then adds it to an array and log the new array
  • If it does exist then log the message saying that it already exists to an array

Solution :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons