+ 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?

8th Apr 2022, 7:49 PM
Alexander Sokolov
Alexander Sokolov - avatar
19 Answers
+ 4
Alexander Sokolov How to replace duplicate elements in an array? Ans : https://code.sololearn.com/cJa7MrLSZPaQ/?ref=app 👇👇👇👇
9th Apr 2022, 9:02 AM
ƬIMΛ
ƬIMΛ - avatar
+ 3
Is that you are always replacing with 2?
8th Apr 2022, 8:29 PM
Faisal Issaka
Faisal Issaka - avatar
+ 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?
8th Apr 2022, 8:55 PM
Jayakrishna 🇮🇳
Jayakrishna 🇮🇳 - avatar
+ 2
Jayakrishna🇮🇳 Yes it would be nice, thanks dude
8th Apr 2022, 9:11 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 1
Faisal Issaka no difference, the main thing is to replace the duplicate in the array with something else.
8th Apr 2022, 8:30 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 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 ]
8th Apr 2022, 8:37 PM
Ipang
+ 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.
8th Apr 2022, 8:38 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 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?
8th Apr 2022, 8:41 PM
Ipang
+ 1
Ipang Yes, you are right, but I don't know how to do it...
8th Apr 2022, 8:43 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 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...
8th Apr 2022, 8:57 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 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..
8th Apr 2022, 9:09 PM
Jayakrishna 🇮🇳
Jayakrishna 🇮🇳 - avatar
+ 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
9th Apr 2022, 6:41 PM
ODLNT
ODLNT - avatar
+ 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!
9th Apr 2022, 10:00 PM
Sanjay Kamath
Sanjay Kamath - avatar
0
Jayakrishna🇮🇳 I mean could you show it in the sololearn code and send it to me here? Thank you for your attention.
8th Apr 2022, 9:00 PM
Alexander Sokolov
Alexander Sokolov - avatar
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..
8th Apr 2022, 9:19 PM
Jayakrishna 🇮🇳
Jayakrishna 🇮🇳 - avatar
0
you could try splice() and indexOf(). I gave a sample code in a comment under ƬIMΛ 's code
9th Apr 2022, 6:32 PM
Bob_Li
Bob_Li - avatar
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.
9th Apr 2022, 7:04 PM
Jayakrishna 🇮🇳
Jayakrishna 🇮🇳 - avatar
0
Swift 4 35.1 la 4 th question kana answer tharnjavanga sollunga please
10th Apr 2022, 9:48 AM
sindhu
sindhu - avatar
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!!
10th Apr 2022, 2:15 PM
Nikhita
Nikhita - avatar