xxxxxxxxxx
const arrOfObjs = [
{
name: 'someName',
tags: [
'sci-fi',
'strategy',
'sandbox',
]
},
{
name: 'someOtherName',
tags: [
'sci-fi',
'mining',
'woodcutting',
'sandbox'
]
}
];
let uniqueTags = [new Set(arrOfObj.map(obj => obj.tags).flat(1))];
// ['sci-fi', 'strategy', 'sandbox', 'mining', 'woodcutting']
xxxxxxxxxx
var myArray = ['a', 1, 'a', 2, '1'];
let unique = [new Set(myArray)];
console.log(unique); // unique is ['a', 1, 2, '1']
xxxxxxxxxx
const categories = ['General', 'Exotic', 'Extreme', 'Extreme', 'General' ,'Water', 'Extreme']
.filter((value, index, categoryArray) => categoryArray.indexOf(value) === index);
This will return an array that has the unique category names
['General', 'Exotic', 'Extreme', 'Water']
xxxxxxxxxx
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> array(n);
for (int i = 0; i < n; i++) {
cin >> array[i];
}
unordered_set<int> distinctNumbers;
vector<int> count(n + 1, 0);
for (int i = n - 1; i >= 0; i--) {
distinctNumbers.insert(array[i]);
count[i + 1] = distinctNumbers.size();
}
for (int i = 0; i < m; i++) {
int li;
cin >> li;
if (li > n) {
cout << 0 << endl;
} else {
cout << count[li] << endl;
}
}
return 0;
}