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
3 1475
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
All you have to do is sort the array numerically. - @sorted_list = sort {$a <=> $b} @unsoted_list;
See the sort() man page for details or the "Sorting Data with Perl - Part One" article in the Howto section of this website.
On second look, I think I misunderstood your requirements. The sample data will sort as needed just by sorting it numerically, but real data probably will not. A hash of arrays would work.
Ashim,
You can try the following script: -
use strict;
-
use warnings;
-
-
my @final; my @duplicates; my @single;
-
my %hash=();
-
my @a=(11,34,55,67,88,101,97,88,11,11,34,83,101,67); ##your array
-
-
foreach(@a)
-
{
-
-
if(exists $hash{$_}) ##check if the element was processed before
-
{
-
push @{$hash{$_}},$_ ;
-
}
-
else {
-
$hash{$_}= [$_];
-
}
-
}
-
-
foreach(keys %hash) {
-
if(@{$hash{$_}}>1) {
-
push @duplicates,@{$hash{$_}}; ##take duplicates into this array
-
}
-
else {
-
push @single,@{$hash{$_}}; ##take single elements in this array
-
}
-
}
-
@final = sort {$a <=> $b} @duplicates;
-
push @final,@single;
-
-
###Result #####
-
print "$_\t" foreach(@final);
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michelle |
last post by:
hi,
i have created an array from recordset containing user names
eg. (davidp, davidp, evenf, patricka, rebeccah)
which i have sorted in alphabetical order, but i need to
identify duplicates...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: cdg |
last post by:
Could anyone tell me how to write a loop for an array, that would have to
check each iteration for duplications of previous entered values. So, the
exact number of loops is not known, but the total...
|
by: Pao |
last post by:
My code works in this way:
I declared a static array in a class (public static int GVetRandom =
new int;)
that in a for cycle I fill with random numbers.
The array gets cleared (clear method) and...
|
by: dawnerd |
last post by:
Hello everyone.
I have a question, or problem if you will, that I'm sure someone knows
the answer to. I have a database that stores information on a given
user. The information is stored in a...
|
by: ak |
last post by:
Is it possible to find repeated(duplicate) element in an array in
single loop ?
AK
|
by: fluk |
last post by:
Hi Guys, I hope someone can help me with this, because i'm getting
crazy to find a good way to do that!
This is what I got by querying a db.
$arr1 = array("site", "description", "area1" ,...
|
by: chemlight |
last post by:
I'm trying to figure out how to order an array based on the number of duplicates, and then remove all duplicates.
This is my code right now:
foreach($acID as $searchterms){
$results =...
|
by: Thekid |
last post by:
I'm trying to figure out a way to find if there are duplicates in an array. My idea was to take the array as 'a' and make a second array as 'b' and remove the duplicates from 'b' using 'set' and then...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |