How to Find the Repeated Number of an Array

const findTheNumber = (array: number[]) => {
  const dict = new Map()
  return array.find((item) => {
    if (dict.has(item)) {
      return item
    } else {
      dict.set(item, true)
    }
  })
}

1. What is the Difference between Map and Object?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#objects_vs._maps

  1. the key of the map can be any type, but the key of the object only can be string or symbol.
  2. the map has order.

The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

2. How to Get the Target Item from a Array?

use the find api of the Array Array.prototype.find()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

The find() method of Array instances returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

how to use:

// find the first item that can be divided by 2(even)
const array = [1, 2, 3, 4]
const target = array.find((item) => {
  return item / 2 === 0
})