-5

How to iterate loop and return the original data from duplicated

I need to use it only for loop.

Array >
0: 2019
1: 2019
2: 2020
3: 2019
4: 2016
5: 2016
6: 2016
7: 2019
8: 2019
9: 2019
10: 2019


I need only [2019,2016,2020];

I don't want the duplicated values.

I hope you guys can help

xKobalt
  • 1,498
  • 2
  • 13
  • 19
  • You need to iterate all the array data to get the values, then, for each value, use another array to store data without duplicated values – xKobalt Feb 28 '20 at 08:37

1 Answers1

0

Something like this:

const uniqueYears = []
years.forEach(year => {
    if(!uniqueYears.includes(year)){
        uniqueYears.push(year)
    }
});

Demo:

 const years = [
    2019,
    2019,
    2020,
    2019,
    2016,
    2016,
    2016,
    2019,
    2019,
    2019,
    2019
];


const uniqueYears = []
years.forEach(year => {
    if(!uniqueYears.includes(year)){
        uniqueYears.push(year)
    }
});

console.log(uniqueYears);
cthmsst
  • 374
  • 1
  • 13