Arranging an array so that duplicates will turn up first

Hi All,

I have an array that contains duplicates as well unique numbers.
ex- (21, 33, 35, 21, 33, 70, 33, 35, 50)

I need to arrange it in such a way that all the duplicates will come up first followed by unique numbers.

Result for the given example should be:
(21, 21, 33, 33, 35, 35, 70, 50)

This a trivial problem in C, creating a binary search tree from the array solves the problem.

As I am new to Perl I don't know indepth of the language.
Would appreciate any help on this regard.

~Ashim

why bother with constructing binary search tree yourself when Perl provides an easy solution:

#!/usr/bin/perl
# dup_array.pl
my @nums = (21, 33, 35, 21, 33, 70, 33, 35, 50);
print "before sorting\n";
for $i (@nums) {
    print $i, "\n";
}

@nums = sort {$a <=> $b} @nums;

print "after sorting\n";
for $i (@nums) {
    print $i, "\n";
}

I _think_ the OP wants:

21, 21, 33, 33, 35, 35, 70, 50

not:

21, 21, 33, 33, 35, 35, 50, 70

Yes, what I really want is all the duplicate members followed by unique numbers without any relational order consideration.

So the Sorting might give a correct solution for this example.

But it is not a generic one.It may happen that a duplicate number is present that is greater than an unique number

if you are not a newbie to Perl, this will guide you to achieve what you want
edit: i was being paranoid here.

this may be more appropriate