JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

1,147,788 Responses

  1. Five Killer Quora Answers To Upvc Window Repairs Upvc Window Repairs

  2. The 10 Most Scariest Things About Upvc Door Repairs Near Me upvc door repairs near me

  3. Renaldo表示:

    What Is Mazda 2 Key Replacement And Why Is Everyone
    Talking About It? mazda key fob; Renaldo,

  4. The Ultimate Guide To Double Glazed Window Repairs Near Me Replacing double glazing glass

  5. JamesNow表示:

    Kamagra 100mg price: buy kamagra online – sildenafil oral jelly 100mg kamagra

  6. Kerry表示:

    To apply for small payday loans on-line with well-reputed, trustworthy lenders, Viva Payday Loans is a loan matching service that’s getting consideration.

  7. slot online表示:

    I was curious if you ever considered changing the layout of your website?
    Its very well written; I love what youve got to say. But maybe you could a little more
    in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one or two images.
    Maybe you could space it out better?

  8. Howdy! This post couldn’t be written much better! Reading through this post reminds me of my
    previous roommate! He continually kept preaching about
    this. I most certainly will send this post to him.
    Pretty sure he’ll have a very good read. Thank you for sharing!

  9. Microsoft Me表示:

    Thanks for sharing such a pleasant idea, post is fastidious,
    thats why i have read it fully

  10. Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Loved it!

  11. JamesNow表示:

    buy Levitra over the counter: Levitra generic price – Vardenafil buy online

  12. CharlesDog表示:

    http://levitrav.store/# Vardenafil online prescription

  13. Puff Wow表示:

    Hi would you mind stating which blog platform you’re working with?
    I’m looking to start my own blog in the near future but I’m having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your design and
    style seems different then most blogs and I’m looking for
    something completely unique.
    P.S Sorry for being off-topic but I had to ask!

    Feel free to visit my homepage: Puff Wow

  14. What’s The Job Market For Which Is The Best Online Supermarket Professionals?

    which is the best online supermarket (http://www.climberf1.com)

  15. health表示:

    This article gives clear idea designed for the new viewers of blogging, that in fact how to do blogging.

  16. CharlesDog表示:

    https://cialist.pro/# Cheap Cialis

  17. The Little-Known Benefits Of Lightweight Folding
    Travel Mobility Scooters foldable Mobility scooter for Adults

  18. CharlesDog表示:

    http://cenforce.pro/# cenforce.pro

  19. look at here表示:

    Hello there! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa?
    My site discusses a lot of the same topics as yours and I believe we
    could greatly benefit from each other. If you might be interested feel free
    to send me an e-mail. I look forward to hearing from you!
    Superb blog by the way!

  20. An All-Inclusive List Of Replacement Key For Nissan Micra Dos And Don’ts replace nissan key
    fob (https://Www.cheaperseeker.com/u/pinfarm46)

  21. GeorgeSep表示:

    Purchase Cenforce Online: Purchase Cenforce Online – order cenforce

  22. 11 Creative Methods To Write About Adult Adhd Medication adhd Medications uk

  23. JamesNow表示:

    buy Levitra over the counter: levitrav.store – Vardenafil buy online

  24. The 10 Most Worst Windows Leeds Fails Of All
    Time Could Have Been Prevented window replacement leeds

  25. CharlesDog表示:

    http://kamagra.win/# Kamagra tablets

  26. Excellent blog! Do you have any suggestions for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost on everything.
    Would you suggest starting with a free platform
    like WordPress or go for a paid option? There are so many options out there that I’m
    totally overwhelmed .. Any ideas? Thanks a lot!

  27. ElmerWes表示:

    Cheap Sildenafil 100mg Cheap Viagra 100mg buy viagra here

  28. CharlesDog表示:

    https://kamagra.win/# buy Kamagra

  29. CharlesDog表示:

    https://levitrav.store/# Cheap Levitra online

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。