+ 2

How transform array1 from to JavaScript

[xxx,yyy,aaa,bbb,xxx,xxx,aaa]=>[xxx1,yyy,aaa1,bbb,xxx2,xxx3,aaa2]

11th Sep 2021, 11:43 AM
Alessio Farroni
Alessio Farroni - avatar
12 Answers
12th Sep 2021, 12:35 AM
Ipang
+ 4
Alessio Farroni Assuming you are talking about the transform of the array ['xxx','yyy','aaa','bbb','xxx','xxx','aaa'] to unique element with running number Here the solution https://code.sololearn.com/cZb9Kdpy78oD/?ref=app
11th Sep 2021, 4:53 PM
Calviղ
Calviղ - avatar
+ 3
Alessio Farroni Please add more information to understand your problem.
11th Sep 2021, 1:00 PM
A͢J
A͢J - avatar
+ 3
Something like this ? a=["xxx","yyy","aaa","bbb","xxx","xxx","aaa"] d={} a.forEach((item,index)=>{ if(item!="bbb"){ if(!(item in d)){ d[item]=1 a[index]=item+1 } else{ a[index]=item+(1+d[item]) d[item]+=1 } } }) console.log(a)
11th Sep 2021, 1:11 PM
Abhay
Abhay - avatar
+ 3
Alessio Farroni doesn't matter . The solution i gave you should work in every case given you understand what is going on .
11th Sep 2021, 1:33 PM
Abhay
Abhay - avatar
+ 2
Alessio Farroni Nice question and challenging too.
11th Sep 2021, 4:31 PM
A͢J
A͢J - avatar
0
//I thik you want to remove duplicate elements from array. var array1 = ['a','b','b','c','a','c','c']; var array2 = [...new Set(array1)]; console.log(array2); // expected output: a,b,c
11th Sep 2021, 1:18 PM
SAN
SAN - avatar
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ i start from an Array with strig if a string Is unique my Array return the string without change else return the string with a progressive Number perhaps if in the array i have text, text return in new Array text1,text2
11th Sep 2021, 1:23 PM
Alessio Farroni
Alessio Farroni - avatar
0
Abhay thanks for your answer, the problem Is the array Is dinamicaly and i don't know if in the array i have bbb or cc or another value
11th Sep 2021, 1:29 PM
Alessio Farroni
Alessio Farroni - avatar
0
Abhay ok, i'll try...thank you very much
11th Sep 2021, 1:36 PM
Alessio Farroni
Alessio Farroni - avatar
0
Calviղ thanks at the Moment i have create this but i think Is a Better way create new Array ti push the value var a=["xxx","yyy","aaa","bbb","xxx","xxx","aaa",'ccc'] var counter = []; function checkDupli (){ var lunghezza = a.length; var dupli= []; for(var i = 0; i < lunghezza; i++){ // itero tutti i valori dell'array for(var j =i+1; j< lunghezza; j++){ // itero tutti i valori dell'array con loro stessi if(a[i]== a[j] && i != j ){ dupli.push(a[j]); counter[a[j]] = 1; a[j]= a[j] + counter[a[j]] counter[a[j]] = counter[a[j]] +1; }else{a[i]= a[i]} } } console.log(dupli); } checkDupli(); console.log(a) Ho quasi vinto
11th Sep 2021, 6:15 PM
Alessio Farroni
Alessio Farroni - avatar