Created
          February 4, 2016 15:20 
        
      - 
      
 - 
        
Save tarzak/1d3676cdc5778efc9e03 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function countClasses(array) { | |
| var object = {} | |
| ; | |
| array.forEach(function(element) { | |
| if (object[element]) { | |
| object[element]++; | |
| } | |
| else { | |
| object[element] = 1; | |
| } | |
| }) | |
| return object; | |
| } | |
| function buildArray(object) { | |
| var array = [] | |
| , maxProperty | |
| ; | |
| while (Object.keys(object).length) { | |
| maxProperty = findMaxProperty(object); | |
| array.push(maxProperty); | |
| delete object[maxProperty]; | |
| } | |
| return array; | |
| } | |
| function findMaxProperty(object) { | |
| var max = 0 | |
| , maxProperty | |
| , property | |
| ; | |
| for (property in object) { | |
| if (object[property] > max) { | |
| max = object[property]; | |
| maxProperty = property; | |
| } | |
| } | |
| return maxProperty; | |
| } | |
| var arr = ['xxx','bad','asd','asd', 'asd', 'bad', 'bad']; | |
| buildArray(countClasses(arr)) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment