Reputation: 1853
In order to find out which element occurs most often in a given array, I've been using the group function from std.algorithm. First I would sort the array (that doesn't seem to be necessary anymore), then pass it to group, and sort the tuple array, so that I could take the first element. It used to work, but now I get the error: overlapping array copy. I'm using version 2.053 for win32. I know that D2 and Phobos are under construction so I'm not really surprised. Just a little puzzled about what's causing the problem.
import std.stdio, std.algorithm, std.array;
void main() {
int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ];
auto a = sort!("a[1] > b[1]")(array(group(arr)))[0];
}
The error I'm getting:
object.Exception@src\rt\arraycat.d(40): overlapping array copy
----------------
42EB58
42E9CF
4061F8
4060A0
406166
405EE8
402072
408898
4088D7
4084D3
4532C9
----------------
The content of the array makes a lot of difference. The following code compiles and prints the correct result:
import std.stdio, std.algorithm, std.array;
void main() {
int[] arr = [ 1, 2, 3, 4, 4, 4, 5 ];
auto a = sort!("a[1] > b[1]")(array(group(arr)))[0];
writeln(a);
}
// prints: Tuple!(int,uint)(4, 3)
Upvotes: 4
Views: 387
Reputation: 68770
This looks to be related to Bug 4789. Your code is correct. See http://d.puremagic.com/issues/show_bug.cgi?id=4789 .
Upvotes: 4