0

Please help me to return the value of the unpaired element from the array[2,3,2,4,5,3,4], that is 5. Please help me with this. Here in the array, only 5 is not having similar pair, all other numbers have the same pair like 2,2 3,3 ...so on.

<head>
<title>Testing JavaScript</title>
</head>
<body>
<script>
console.log("Console unpaired element from array")

const years =[2,3,2,4,5,3,4];
const del=[...new Set(years)];
console.log(del);

</script>
</body>
</html>
aish
  • 69
  • 9
  • 1
    Hi aish, please show us what you have tried and where the error is. Currently it looks like you haven't tried – Aaron Morefield Jun 03 '21 at 17:03
  • 2
    This question needs more information, Are you guaranteed that there will be either `pairs` or no pairs? What happens if there are `[5,5,5,5]`, Does it count as 2 pairs of 5 or will there be only a pair of any numbers if they exist at any point of time in an array? – Sudheesh Singanamalla Jun 03 '21 at 17:04

1 Answers1

0

Assuming we only return values with count = 1

const years =[2,3,2,4,5,3,4];
const t = {}
years.forEach(y=>{
if(t[y])
  t[y] += 1
else
  t[y] = 1
});

console.log(Object.keys(t).filter((k)=> t[k]==1))
Abishek Kumar
  • 519
  • 5
  • 13