View Single Post
Old 12-01-2012, 11:26 PM   PM User | #14
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
A shorter script with Old Pedant observations
Code:
var arr=[1,41,222,33,41,41,41,40,41,18,41,41,41,22,41,2,2,41,42,27,41,58];

var dominant=null,halfLength=arr.length/2,str=arr.sort().join(',').replace(/(,\d+)(\1+)(?=,)/g,
	function(a,b){if (halfLength<=a.length/b.length) {dominant=+b.substr(1)}});

var msg=dominant?'Dominant number : '+dominant:'No dominant number !'
alert(msg);
EDIT : A new corrections repetition followed by a comma or a word boundary. An assertion like (\1+)(?=,|\b) matches a repetition followed by a comma or a word boundary, but does not include the comma in the match. There is never two dominant numbers !

Last edited by 007julien; 12-02-2012 at 12:03 AM.. Reason: complements
007julien is offline   Reply With Quote