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,245,941 Responses

  1. casino pin up表示:

    У каждого слота есть демоверсия, кроме слотов PG Soft.

  2. BrandenSlomy表示:

    buy dapoxetine online: buy priligy max pharm – cheap priligy

  3. A person necessarily lend a hand to make seriously articles I’d state.
    This is the first time I frequented your web page and to this point?
    I surprised with the analysis you made to make this actual submit incredible.
    Wonderful activity!

    my website :: Pinoy SEO Services

  4. Medication information. Generic Name.
    how to buy cheap loperamide tablets
    Some what you want to know about pills. Read here.

  5. AlbertDuM表示:

    stromectol 6 mg tablet: buy Stromectol – ivermectin 500ml

  6. вывод из запоя круглосуточно краснодар на дому dolgoprudni.rusff.me/viewtopic.php?id=3065#p8958 .

  7. вывод из запоя краснодар на дому familyportal.forumrom.com/viewtopic.php?id=28570#p70369 .

  8. Hello There. I found your blog using msn. This is an extremely well written article.

    I’ll make sure to bookmark it and return to read more of your useful information. Thanks for the
    post. I will definitely return.

  9. canopy design表示:

    Fantastic write ups, With thanks!

  10. AlbertDuM表示:

    minocycline 50mg without doctor: buy minocycline 100mg otc – minocycline manufacturer

  11. вывод из запоя в стационаре краснодара zavitai.mybb.social/viewtopic.php?id=90 .

  12. AlbertDuM表示:

    lisinopril pills 2.5 mg: lisinopril1st – Lisinopril 1st

  13. вывод из запоя на дому в краснодаре http://chesskomi.borda.ru/?1-8-0-00003046-000-0-0-1730745020/ .

  14. AlbertDuM表示:

    buy minocycline 50mg: generic Stromectol – buy minocycline 100 mg

  15. online casino表示:

    I revealed your blog post to be a compelling and discerning examination of the present state of the sphere.
    Your investigation of the key shifts and problems
    addressing companies in this sector was outstandingly potent .

    As an devoted advocate of this concern , I would be excited to further this conversation more thoroughly .
    If you are inclined , I would happily implore you to explore
    the captivating options open at WM CASINO.
    Our system delivers a high-tech and secure landscape for interacting
    with aligned enthusiasts and obtaining a profusion of tools to bolster your
    comprehension of this fluctuating sector.
    I anticipate the opportunity of partnering with you in the upcoming
    future

    My homepage: online casino

  16. Nikefut表示:

    darkweb marketplace darknet drug links https://darknetmarketstore.com/

  17. Thanks for the marvelous posting! I definitely enjoyed
    reading it, you may be a great author.I will ensure that I bookmark your blog and
    will often come back down the road. I want to encourage one to continue
    your great work, have a nice holiday weekend!

  18. I am sure this post has touched all the internet users,
    its really really nice piece of writing on building up new webpage.

  19. Full Article表示:

    Fantastic post however , I was wondering if you could
    write a litte more on this subject? I’d be very grateful if you could elaborate a little bit further.
    Bless you!

  20. AlbertDuM表示:

    buy Lisinopril online: Lisinopril 1st – lisinopril 20mg online

發佈留言

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