Mode | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Mode

https://code.sololearn.com/cER0hw0GW0sa/?ref=app I post my code here, The concept is to find common repeated elements in array.. all fine, but logic problem may occur in this code... It prints repeated elements in array, but wants to prints common element from the repeated elements.. For eg., Input : size : 6 1 2 2 3 3 3 Calculated output : 2 3 3 but, Expected Output : 2 3 (common at once) What logic I should follow?

2nd Dec 2019, 8:06 AM
Kumaresan L
Kumaresan L - avatar
6 Answers
+ 1
After printing x[i] in line 45 put i++; to advance next element, to not check again.. take another array for printing, compare element in the array present or not if present dont assign. So print array with no duplicates.... kumaresan or else put simply like this after printing x[i] in if while(x[i]==x[i+1]) i++; it works for any inputs... https://code.sololearn.com/cl9Yc0UgswA7/?ref=app
2nd Dec 2019, 9:16 AM
Jayakrishna 🇮🇳
+ 1
kumaresan I just want to tell you how I would approach this question. 1) First of all sort the array. 2) Initialize a counter variable which will count the repetition of elements. 3) Then increment the "i" value by i+(count-1) and in the inner loop put j=i so that you don't have to traverse from the beginning of the array.
2nd Dec 2019, 8:44 AM
Avinesh
Avinesh - avatar
+ 1
kumaresan Your code sorts in descending order of values. Kindly have a look a this and a part of your code is commented by me, use the logic 2 and 3 given by me in my previous answer to get your solution. https://code.sololearn.com/cyLC7Sp8rtdD/?ref=app
2nd Dec 2019, 9:02 AM
Avinesh
Avinesh - avatar
+ 1
Avinesh Thanks
2nd Dec 2019, 3:30 PM
Kumaresan L
Kumaresan L - avatar
0
2nd Dec 2019, 3:30 PM
Kumaresan L
Kumaresan L - avatar
0
kumaresan welcome...
2nd Dec 2019, 3:45 PM
Jayakrishna 🇮🇳