0

# No. of Duplicated numbers c++

So I want to print out the number of different duplicated numbers for example arr [10] = {1,1,1,1,2,2,2,3,3,3} /* output there are 3 duplicated numbers no matter how many one number has been duplicated. NOTE: No functions allowed

19th Oct 2019, 12:39 PM
light of dark 9
+ 5
I must confess I am a bit confused by the question, but maybe it's because it's kinda obvious to me, Ipang. Anyway, the premise is whenever a new element is added to iterate over all preceding numbers and to count how often the new number is already in the array, and afterwards to compare if it equals one, because if the current number is exactly once in the array, it is a new duplicate number. However, if it didn't appear previously (count == 0), we have a new number, but not a new duplicate, so we don't count it quite yet (you see, if we changed the condition to count == 0, we would obtain the amount of different numbers in the array, e.g. 6 for your sample input). On the other hand, if it is there two or more times, it's a duplicate number, but that also means we registered it before, so we don't count it again. That's about the whole idea, nothing extraordinary.
19th Oct 2019, 3:10 PM
+ 7
You can use STL container map
20th Oct 2019, 1:49 AM
Rishabh Gupta
+ 4
Thank you Shadow I'll try to keep that in mind 👍
19th Oct 2019, 3:20 PM
Ipang
+ 4
Bit by bit I am Shadow , But I'm relieved now you got it : )
19th Oct 2019, 6:40 PM
Ipang
+ 3
Let me see your attempt first?
19th Oct 2019, 1:34 PM
Ipang
+ 3
light of dark 9 Code seems to work alright, except for a typo in `++duplicated;` I think it's alright. Is there any problem you're dealing with?
19th Oct 2019, 2:21 PM
Ipang
+ 3
light of dark 9, the logic of your if statement is flawed. Right now you basically increment whenever the current number has already been entered before (i =! j is always true anyway, not sure what that is for, since you iterate from 0 to i only). What you want to do is increment only if the current number has been entered before exactly once, as that is the point you encounter that duplicate number for the first time. You need another variable which keeps track of this for you. I rewrote your program to incorporate such a variable, have a look at the code: https://code.sololearn.com/cSlL4P2KIc1l/?ref=app
19th Oct 2019, 2:47 PM
+ 3
I tested the code with these numbers 1 2 3 4 5 4 3 2 1 6 And it counted number of duplicated numbers correctly (got 4 duplicated numbers). I'm a bit lost here, why did it work correctly with such inputs Shadow
19th Oct 2019, 2:55 PM
Ipang
+ 3
light of dark 9 You gave nine out of ten numbers allowed, how about giving it all ten? you meant this test with your code right? Shadow's code gave correct calculation with both sample lists BTW.
19th Oct 2019, 4:10 PM
Ipang
+ 3
19th Oct 2019, 4:27 PM
light of dark 9
+ 3
Thanks for the defense, Ipang. ^^ No problem, light of dark 9. :)
19th Oct 2019, 4:29 PM
+ 3
Shadow Just a confirmation, in my previous response, when I tested with 1 2 3 4 5 4 3 2 1 6 numbers, I meant to say I was testing the numbers in light of dark 9's code, not yours 😁
19th Oct 2019, 5:03 PM
Ipang
+ 3
Ooooh, he, then I got that absolutely wrong, sorry for that, Ipang. XD Well, I suppose you handtraced that by now?
19th Oct 2019, 6:34 PM
+ 1
If your inputs were: 1 1 2 2 3 3 4 5 6 it will work well but if your inputs were 1 1 1 2 2 3 3 4 5 it wont Ipang Shadow
19th Oct 2019, 4:02 PM
light of dark 9
+ 1
Shadow Not at all, I'm still trying to digest your code BTW hehehe light of dark 9 No problem, Shadow should get the credits though : )
19th Oct 2019, 4:37 PM
Ipang
20th Oct 2019, 7:34 AM
Eeshan Panse
0
#include <iostream> using namespace std; int main() { int i, j, duplicated = 0, MyArr[10]; for (i = 0; i<10; i++) { cin >> MyArr [i]; for (j = 0; j < i; j++) { if (i != j && MyArr[i] == MyArr[j]) { ++ diplicated; } } } cout << "There is/are "<<duplicated<<"duplicated number(s)"<<endl; return 0; } Ipang
19th Oct 2019, 1:43 PM
light of dark 9
0
it shows that i have duplicated 1 for two times n 4 for three times like this and i don't want that i want it to count for example number 4 only once enev if i repeat it ten times ... because i am not interested in how many times you have repeated a single number, i am interesting in how many numbers been repeated only
19th Oct 2019, 2:41 PM
light of dark 9