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,478,770 Responses

  1. The Basics Of A Bridging Loan 다바오 밤문화

  2. I don’t even know the way I finished up right here,
    however I believed this submit was great. I don’t recognise who you might
    be however definitely you are going to a famous blogger if you aren’t
    already. Cheers!

  3. лечение наркозависимости в стационаре https://www.ideya.forums.party/viewtopic.php?id=661 .

  4. I just had my roof replaced, and this article was super helpful in understanding the process! For those considering a replacement, check out Roofer Near Me for great resources

  5. boobs表示:

    Hey There. I found your blog using msn. This is an extremely well written article.
    I will make sure to bookmark it and come back to
    read more of your useful information. Thanks for the post.
    I’ll definitely return.

  6. вывод из запоя в стационаре воронежа zal.rolevaya.info/viewtopic.php?id=5384 .

  7. Guide To Gspot Rabbit Toy: The Intermediate Guide Towards Gspot Rabbit Toy
    gspot Rabbit toy

  8. Pol88表示:

    I’m no longer positive where you’re getting your information, but good topic.
    I must spend some time finding out more or working out more.
    Thank you for fantastic info I used to be looking for this information for my mission.

  9. Nikefut表示:

    how to get on dark web tor markets links the dark internet

  10. I wanted to thank you for this excellent read!! I absolutely loved every little
    bit of it. I’ve got you book marked to check out
    new things you post…

    Also visit my site … Realtor in Meridian Mississippi

  11. вывод из запоя в стационаре анонимно http://www.mozaisk.anihub.me/viewtopic.php?id=4371/ .

  12. Drug prescribing information. Cautions.
    where to buy levitra without insurance
    Actual what you want to know about medicament. Get now.

  13. Why visitors still use to read news papers when in this technological globe all
    is presented on net?

  14. Terrific article! This is the type of information that should be shared around the
    internet. Disgrace on Google for now not positioning this submit higher!
    Come on over and discuss with my web site .
    Thank you =)

    My webpage :: the growth matrix program free download

  15. I know this if off topic but I’m looking into starting my
    own blog and was curious what all is needed to get set
    up? I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet savvy so I’m not 100% positive. Any suggestions
    or advice would be greatly appreciated. Cheers

    Feel free to visit my website – Live Casino House

  16. Russian shop表示:

    The other day, while I was at work, my sister stole my apple ipad
    and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views.
    I know this is entirely off topic but I had to
    share it with someone!

    Take a look at my web page: Russian shop

發佈留言

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