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,365,693 Responses

  1. koitoto togel表示:

    Very shortly this web page will be famous amid all blogging people, due
    to it’s nice content

    Stop by my web blog: koitoto togel

  2. Raquel表示:

    7 Simple Secrets To Completely You Into Window Glass Repairs
    repairing glass (Raquel)

  3. Five Killer Quora Answers On Link Togel Resmi link Togel resmi

  4. The Best Pragmatic Free Trial Meta Techniques For Changing Your Life 프라그마틱 무료스핀 (Warren)

  5. В магазине сейфов предлагают где купить сейфы в москве сейфы простые

  6. ta88表示:

    Admiring the persistence you put into your website and in depth
    information you provide. It’s nice to come across a blog every once in a while
    that isn’t the same old rehashed information. Wonderful read!
    I’ve saved your site and I’m adding your RSS feeds to my Google account.

  7. 11 “Faux Pas” You’re Actually Able To Create With Your Wall.Mounted Fireplace fireplaces on wall (Ruby)

  8. BarryDof表示:

    http://rybelsus.shop/# buy rybelsus online

  9. Jeremypag表示:

    rybelsus price semaglutide online buy rybelsus online

  10. Frankevild表示:

    https://rybelsus.shop/# rybelsus price

  11. ads508 link表示:

    You can definitely see your expertise in the article you write.
    The world hopes for even more passionate writers such as you who are not afraid to mention how they believe.
    All the time follow your heart.

  12. The 10 Most Popular Pinterest Profiles To Keep Track Of Best Kids’
    Bunk Beds bunk beds for kids – cplsportst11.Com,

  13. Dorthea Caird表示:

    Nine Things That Your Parent Taught You About Audi Keys audi car key case

  14. Kristofer Eck表示:

    The 10 Most Terrifying Things About Best Bunk Bed Design Best Bunk bed Design

  15. Sarah表示:

    Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something.
    I think that you could do with some pics to drive the message home a bit, but instead
    of that, this is great blog. An excellent read. I’ll definitely be back.

  16. Thanks for the auspicious writeup. It in truth was once
    a entertainment account it. Look advanced to more introduced agreeable from you!
    By the way, how can we communicate?

  17. Sue Poston表示:

    10 Websites To Help You To Become An Expert In Upvc Window Handle Replacement Double Glazing Window Handle

  18. RamonRek表示:

    buy ozempic: buy ozempic pills online – ozempic cost

  19. I do trust all of the ideas you’ve offered for your post.
    They are really convincing and can certainly work. Nonetheless, the posts are very brief for newbies.
    Could you please prolong them a bit from next time?
    Thanks for the post.

  20. 5 Common Phrases About Electric Fireplace Wall Mounted You Should Avoid slimline wall mounted electric fires, Claire,

  21. В магазине сейфов предлагают сейфы сейфы

  22. Frankevild表示:

    https://rybelsus.shop/# rybelsus pill

  23. Профессиональный сервисный центр по ремонту планшетов в Москве.
    Мы предлагаем: ремонт планшетов на дому
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  24. BarryDof表示:

    https://ozempic.art/# ozempic generic

  25. website表示:

    Wonderful work! This is the type of information that are supposed to be shared around
    the net. Disgrace on the seek engines for now not positioning this put up higher!
    Come on over and discuss with my site . Thank you =)

  26. The Best Sports Toto Korea It’s What Gurus Do Three Things 첫가입 꽁머니

  27. Frankevild表示:

    http://ozempic.art/# Ozempic without insurance

發佈留言

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