473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jan 24 '08 #1
3 1475
KevinADC
4,059 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. @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.
Jan 24 '08 #2
KevinADC
4,059 Expert 2GB
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.
Jan 24 '08 #3
nithinpes
410 Expert 256MB
Ashim,
You can try the following script:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my @final; my @duplicates; my @single;
  5. my %hash=();
  6. my @a=(11,34,55,67,88,101,97,88,11,11,34,83,101,67); ##your array
  7.  
  8. foreach(@a)
  9. {
  10.  
  11.   if(exists $hash{$_})   ##check if the element was processed before
  12.   { 
  13.      push @{$hash{$_}},$_ ; 
  14.   } 
  15.  else {
  16.   $hash{$_}= [$_];
  17. }    
  18. }
  19.  
  20. foreach(keys %hash) {
  21. if(@{$hash{$_}}>1) {
  22. push @duplicates,@{$hash{$_}}; ##take duplicates into this array
  23. }
  24.  else {
  25. push @single,@{$hash{$_}}; ##take single elements in this array
  26. }
  27. }
  28. @final = sort {$a <=> $b} @duplicates;
  29. push @final,@single;
  30.  
  31. ###Result #####
  32. print "$_\t" foreach(@final);
  33.  
Jan 25 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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...
58
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...
11
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...
6
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...
6
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...
14
by: ak | last post by:
Is it possible to find repeated(duplicate) element in an array in single loop ? AK
5
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" ,...
2
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 =...
3
Thekid
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
BarryA
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...
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.