Tell me how to iterate through the array to replace duplicates with empty strings. You need to leave one unique value each, and replace duplicates with an empty string.
let arr= [1, 2, 3, 1, 3, 1, 5];
In this example array, it should look like this:
let arr= [1, 2, 3, '', '', '', 5];
@AlekseyShymansky is better Set
Alexey Ten2022-02-12 23:21:15@Alexey Ten can you show an example with Set?
Александр2022-02-12 23:21:15@Alexander wouldn't it be better to just remove the duplicates?
Проста Miha2022-02-12 23:21:15Here's a one-liner res=arr.map(function(v){return this.has(v)?'':(this.add(v),v)},new Set) ????
Alexey Ten2022-02-12 23:21:15-
Answer # 1
For an efficient replacement (in linear time), you should use
set
:In-place replacement:
var a= [1, 2, 3, 1, 3, 1, 5] var used= new Set() for (var q=0; q<a.length; ++q) { if (used.has(a[q])) { a[q]= "" } else { used.add(a[q]) } } console.log(a)
With the creation of a new array:
var a= [1, 2, 3, 1, 3, 1, 5] var used= new Set() varb= a.map(x=> used.has(x) ? "" : (used.add(x), x)) console.log(b)
And another cool trick as a bonus:
var a= [1, 2, 3, 1, 3, 1, 5] var i= 0 for (var x of new Set(a)) { while (a[i++] !== x) { a[i-1]= "" } } for (; i < a.length; ++i) { a[i]= "" } console.log(a)
-
Answer # 2
For an efficient replacement (in linear time), you should use
set
:In-place replacement:
var a= [1, 2, 3, 1, 3, 1, 5] var used= new Set() for (var q=0; q<a.length; ++q) { if (used.has(a[q])) { a[q]= "" } else { used.add(a[q]) } } console.log(a)
With the creation of a new array:
var a= [1, 2, 3, 1, 3, 1, 5] var used= new Set() varb= a.map(x=> used.has(x) ? "" : (used.add(x), x)) console.log(b)
And another cool trick as a bonus:
var a= [1, 2, 3, 1, 3, 1, 5] var i= 0 for (var x of new Set(a)) { while (a[i++] !== x) { a[i-1]= "" } } for (; i < a.length; ++i) { a[i]= "" } console.log(a)
-
Answer # 3
Here is the variant with
set
and without quadratic complexity:const arr= [1, 2, 3, 1, 3, 1, 5]; const seen= new Set(); const res= arr.map((val)=> { if (seen.has(val)) { return ''; } seen.add(val); return value; }); console log(res);
Oh, I almost wrote the same
Qwertiy2022-02-12 23:21:15 -
Answer # 4
Here is the variant with
set
and without quadratic complexity:const arr= [1, 2, 3, 1, 3, 1, 5]; const seen= new Set(); const res= arr.map((val)=> { if (seen.has(val)) { return ''; } seen.add(val); return value; }); console log(res);
Oh, I almost wrote the same
Qwertiy2022-02-12 23:21:15 -
Answer # 5
Here's my version, I hope it helped
let arr= [1, 2, 3, 1, 3, 1, 5]; for(let i= 0; i < arr.length; i++){ for(let j= i + 1; j < arr.length; j++){ if(arr[i]== arr[j]) arr[j]= ""; } } console.log(arr);
-
Answer # 6
const arr= [1, 2, 3, 1, 3, 1, 5]; const res= []; for (let i= 0; i < arr.length; i++) { if (res.includes(arr[i])) { res push(''); } else { res push(arr[i]); } } console.log(res);
Here is a working, but not the best option. I think it's good for you to understand.
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
use an additional array and the Includes method, which will check for the presence of this value in the existing array (where the values \u200b\u200bwill be "shifted" to)
Алексей Шиманский2022-02-12 23:21:15