Wednesday 15 July 2015

c++ - Function to return count of duplicate numbers in sorted array -


I want to return the number of duplicate values ​​in a sorted array.

For example: a = {1, 1, 2, 3, 4, 4}, Freteli (N) 2 should be returned. (They 1, 1 and 4, 4)

I tried to use a recursive approach, but it does not work, it always gives me 4.

I'm asking if someone can help me understand the better way of programming. Thanks a lot!

Function: #include & lt; Iostream & gt; using namespace std; Int fratelli (int a [], int l, int r) {if (l == r) 0; Else {int c = (l + r) / 2; Int n = fratelli (a, l, c) + fracty (a, c + 1, r); If (a [L] == one [L + 1]) +++; Return n; }} Int main () {const int _N = 11; Int array [_N] = {1, 1, 2, 3, 5, 5, 7, 8, 8, 11, 12}; Cout & lt; & Lt; "\ N" & lt; & Lt; Fratelli (array, 0, _n-1); Return 0; }

You have an error on this line:

 < Code> if (a [L] == one [L + 1]) +++;   

The check index should be c not l . Besides, your code sounds fine for me.

No comments:

Post a Comment