+ 2

Can anyone solve this program?

Challenge-Program to delete duplicates in a array. Intitial array- A []={6,1,3,5,6,1,3,9} Final array-A []={6,1,3,5,9}

24th Mar 2018, 5:28 PM
Napster
Napster - avatar
4 Réponses
+ 19
//just thought , simple approach 👍 1)sort your array , so that you are sure ... repeated number will be adjacent to each other 2)then, run a loop through it & next 1 is equal to current1 then change value of current one to 0 & count how many times this happens 3) make array of size of initial array - no. of times 0 there & run a loop to add elements from initial loop to it [not 0 ,then increase index no.]
24th Mar 2018, 6:04 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
@Ariela Did it work?
24th Mar 2018, 5:38 PM
Napster
Napster - avatar
+ 1
I’m not testing, but it should be okay. The syntax is correct for a fact and the algorithm is definitely working. If anything just google the correct method names for the length of an array and adding elements to an array
24th Mar 2018, 5:39 PM
Ariela
Ariela - avatar
0
Are you trying to remove repeating elements? int[] initial = {6,1,3,5,6,1,3,9}; int[] final; for(int i=0;i < initial.length;i++){ if(!final.contains(initial[i]){ final.push(initial[i]); } } I’m not sure if the method names are correct. I haven’t used java in a little while.
24th Mar 2018, 5:35 PM
Ariela
Ariela - avatar