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,479,157 Responses

  1. Купить дженерики с доставкой по Санкт-Петербургу качественные препараты для повышения потенции
    на сайте https://men78.ru высокое качество производства Индии в наличии в СПБ

  2. выведение из запоя воронеж стационар https://stranaua.ukrbb.net/viewtopic.php?f=2&t=59768 .

  3. This design is incredible! You obviously know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job.
    I really loved what you had to say, and more than that, how you presented it.
    Too cool!

  4. лечение наркозависимости в стационаре gaslo.ukrbb.net/viewtopic.php?f=13&t=3413 .

  5. выведение из запоя воронеж стационар https://www.family2.quadrobb.me/viewtopic.php?id=1841 .

  6. Nikefut表示:

    darknet market dark market link deep web links

  7. выведение из запоя воронеж стационар https://planeta.mybb.social/viewtopic.php?id=2229/ .

  8. BrandenSlomy表示:

    can i buy clomid for sale: clomid purchase online rex pharm – how to get cheap clomid online

  9. AlbertDuM表示:

    cytotec online: cyt premium – buy cytotec online

  10. I don’t know if it’s just me or if perhaps everyone else
    experiencing problems with your blog. It appears like some of the text within your posts
    are running off the screen. Can somebody else please comment and let me know if this
    is happening to them as well? This may be a issue with my web browser because I’ve had this happen previously.
    Thank you

  11. BrandenSlomy表示:

    where to buy clomid without dr prescription: buy clomid – can i purchase generic clomid without dr prescription

  12. It’s really very complicated in this full of activity life to listen news on Television, so I
    simply use the web for that reason, and take the most up-to-date news.

  13. выведение из запоя воронеж стационар justforum.bestforums.org/viewtopic.php?f=26&t=4753 .

  14. вывод из запоя в стационаре воронежа http://www.gaslo.ukrbb.net/viewtopic.php?f=13&t=3413/ .

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

  16. Jorge Allison表示:

    How do I find the best deals on tours in Dubai? Recommendations for a trusted dhow cruise dubai marina offers would be

  17. There are various tools and websites that affirmation to allow users
    to view private instagram viewer without human verification Instagram profiles,
    but it’s important to approach these taking into consideration caution. Many of these tools can be unreliable, may
    require personal information, or could violate Instagram’s terms of service.

    Additionally, using such tools can compromise your own security or
    guide to scams. The safest and most ethical way to view a private profile is to send a follow demand directly to the
    user. Always prioritize privacy and glorification in your online interactions.

  18. That is really attention-grabbing, You are an excessively skilled blogger.
    I’ve joined your rss feed and look forward to in search
    of extra of your fantastic post. Additionally, I’ve
    shared your site in my social networks

  19. BrandenSlomy表示:

    60 mg prednisone daily: prednisone 4 mg daily – prednisone pack

  20. These drawbacks are all limited.

  21. Winifred表示:

    Concluded Reading a Blog Post: A Formal Reply to
    the Comment Section and an Invitation to Join “KING855”

    ‘After thoroughly perusing the blog post, I would like to submit the
    following feedback to the discussion .

    Your perspectives regarding the theme were quite intriguing .
    I was in agreement with a number of the claims
    you articulated.

    It is gratifying to witness such an stimulating dialogue taking place
    .

    If you are interested in additional investigating
    this subject matter , I would sincerely encourage you to become a member of the “KING855” network .
    There , you will have the chance to engage with kindred spirit
    participants and dive deeper into these intriguing topics
    .

    I am confident your contribution would be a meaningful
    addition to the dialogue.

    Appreciate your input , and I anticipate the prospect of extending this stimulating conversation.

    Also visit my web site: online casino software providers, Winifred,

發佈留言

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