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
18 odpowiedzi
+ 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.
+ 7
You can use STL container map
+ 4
Thank you Shadow
I'll try to keep that in mind 👍
+ 4
Bit by bit I am Shadow ,
But I'm relieved now you got it : )
+ 3
Let me see your attempt first?
+ 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?
+ 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
+ 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
+ 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.
+ 3
Thanks for the defense, Ipang. ^^
No problem, light of dark 9. :)
+ 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 😁
+ 3
Ooooh, he, then I got that absolutely wrong, sorry for that, Ipang. XD
Well, I suppose you handtraced that by now?
+ 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 : )
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
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