JSFiddle - React, Tailwind, and code Playground
by ysuper
HTML
<h2>
集合 找出 重覆元素
</h2>
JavaScript
var origin = [1, 2, 'a', 3, 1, 'b', 'a'];
/* var origin = [2, 3, 1, 'b', 'a']; */
var result = new Set();
var repeat = new Set();
origin.forEach(item => {
result.has(item) ? repeat.add(item) : result.add(item);
})
console.log(result); // {1, 2, "a", 3, "b"}
console.log(repeat); // {1, "a"}
console.log(repeat.size);
if (repeat.size != 0) {
console.log(repeat.values());
for (let v of repeat.values()) {
console.log(v);
}
}