Program to remove duplicate elements in an array in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Program to remove duplicate elements in an array in C

I want to code a program which takes about 10000 numbers which maybe duplicated, I want to remove the duplicated numbers and then add them up but I would have runtime exceed, How can I do that in C language?

23rd May 2020, 10:45 PM
Nika Soltani Tehrani
Nika Soltani Tehrani - avatar
5 Answers
+ 4
ahhh....thought.....qsort() first then.........instead of having a second array....if current == next, replace current with a 0 (zero) in the same array then loop through to do the sum. Again, no idea how long it would take. Is removing the duplicates the main objective or are you just trying to get sum of the none duplicates?
24th May 2020, 12:33 AM
rodwynnejones
rodwynnejones - avatar
+ 3
HonFu Actually I have to code for somewhere and they had made this time limitation. Im looking for another way to solve this problem instead fo using nested for
24th May 2020, 12:10 AM
Nika Soltani Tehrani
Nika Soltani Tehrani - avatar
+ 3
You could use qsort() to sort them, then... in a single loop, check if the current is equal to next......if it isn't...... put the current into second array..(but I have no ideal how long that would take)....then loop through the second array to do the sum. (still thinking about it thought.)
24th May 2020, 12:22 AM
rodwynnejones
rodwynnejones - avatar
+ 2
Runtime exceeded is a Sololearn issue because the runtime for every code here is limited to a few seconds. Did you try the same code on your computer?
23rd May 2020, 11:39 PM
HonFu
HonFu - avatar
+ 2
rodwynnejones haven’t heard about qsort() function Really interesting. Your opinion about making the duplicated ones to zero was awesome Thank you so much🙏😊 I’ll try that
24th May 2020, 12:48 AM
Nika Soltani Tehrani
Nika Soltani Tehrani - avatar