查询数组中是否有重复的元素(js)

2017-05-24  本文已影响20人  千罹
const array = ['a', 'b', 'c'];  // 数据源

function checkRepeat(array) {
  const temp = {};                // 临时表
  const repeat = {};              // 重复的数据
  for (const i of array) {
    if (temp[i]) {
      repeat[i] = true;
    } else {
      temp[i] = true;
    }
  }
  return repeat;
}
上一篇 下一篇

猜你喜欢

热点阅读