+ 5
help plz i need help with this!
how to replace duplicate elements in an array? For example: var arr = [1, 1, 3, 4]; => 1 replace on 2 => var arr = [1, 2, 3, 4]; Who can solve this problem and help me?
19 Réponses
+ 4
Alexander Sokolov How to replace duplicate elements in an array?
Ans :
https://code.sololearn.com/cJa7MrLSZPaQ/?ref=app
👇👇👇👇
+ 3
Is that you are always replacing with 2?
+ 2
You can replace dublicate with max_value+1 so that original won't get changed in its first single position..
It only changes dublicates and also don't create dublicates...
edit:
Alexander Sokolov
[ 1,1,3,4] => [ 1,5,3,4 ]
[ 1, 1, 3, 4, 4, 4, 5 ]
=>
[ 1, 6, 3, 4, 7, 8, 5]
is this works?
+ 2
Jayakrishna🇮🇳 Yes it would be nice, thanks dude
+ 1
Faisal Issaka no difference, the main thing is to replace the duplicate in the array with something else.
+ 1
What if there are multiple duplicate values in different positions? what should be done?
[ 1, 1, 3, 4, 4, 4, 5 ]
[ 1, 2, 3, 4, 2, 6, 2 ]
+ 1
Ipang replace them with something else, the main thing is that there are no duplicates in the array. I do not know how to do it.
+ 1
You mean, just generate a random value to replace it with?
Should the final result appear in sorted form, ascending order, like the example on original post?
+ 1
Ipang Yes, you are right, but I don't know how to do it...
+ 1
Jayakrishna🇮🇳 could you show it in code? I think it's a very interesting idea, but I don't quite understand how it should look in code...
+ 1
Alexander Sokolov I can code in java but difficult to in javascript now, I don't remember array methods , there are lot. But I can give you algorithm if you want... You can code it very easy if you find methods..
+ 1
Alexander Sokolov
Based on this statement "Ipang replace them with something else, the main thing is that there are no duplicates in the array. I do not know how to do it."
and the algorithm provided by Jayakrishna🇮🇳
https://code.sololearn.com/czRya9k0JIFk
+ 1
Alexander Sokolov From what I understand you need a series with seed 1 increment 1.
1.Clear the array var to blank array - 0 elements
2.Reinitialise the array to the required length (dimension)
and populate it with the new series.
I think this is okay!
0
Jayakrishna🇮🇳 I mean could you show it in the sololearn code and send it to me here? Thank you for your attention.
0
Use a loop to traverse array elements..
If current elements is in new array already then replace with max_value+1 and replace curent value in original array with max_value+1.
else just push current item.
Reapeat this same till end.
May be here array partitions, splice or slice may help along with max (array)...
May be there are methods which can simplify your task I guess.. It's not difficult once you find methods..
edit:
Alexander Sokolov
just with indexof() method which returns -1 if not exist, and max_value by Math.max(...arr), you can done the job here...
Hope it helps..
0
you could try splice() and indexOf().
I gave a sample code in a comment under ƬIMΛ 's code
0
var arr = [1,1,4,4,1,2,2,4]
var a = [];
for (var i=0; i<arr.length; i++)
if (a.indexOf(arr[i]) == -1)
a.push(arr[i]);
else
a.push(Math.max(...arr,...a)+1)
console.log(a);
//this is what I mean in simple way..
//is it not solved yet? hope it helps.
0
Swift 4 35.1 la 4 th question kana answer tharnjavanga sollunga please
0
arr[1]=2; assigning the value directly in the place of duplicate!! is this what u meant? replacing the value at index of duplicate!!