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,238,584 Responses

  1. duatoto表示:

    My brother suggested I might like this web site. He was totally right.
    This post actually made my day. You cann’t imagine simply how much time I had spent for this information! Thanks!

  2. Uazrzyh表示:

    Полезные советы по безопасной покупке диплома о высшем образовании

    dvoinoi-brus.ru/kupit-attestat-za-11-klass-v-volzhskom.html

  3. A youtube mp3 download tool is a free web application that allows
    users to download videos from youtube.

  4. LarryCoell表示:

    matadorbet.bid matadorbet bid matadorbet.bid

  5. Hello, I enjoy reading all of your post. I like to write a little comment to support
    you.

  6. 9 Lessons Your Parents Taught You About Cheap Sleeper Sofas cheap sleeper sofas

  7. CarrollUnton表示:

    deneme bonusu veren siteler yeni https://slot-tr.online/# slot tr online

  8. The Reasons To Focus On Improving Renault Replacement Key Card renault trafic replacement
    key (https://hikvisiondb.webcam)

  9. Diplomi_awer表示:

    купить диплом в великих луках [url=https://prema365-diploms.ru/]prema365-diploms.ru[/url] .

  10. WnrPaish表示:

    Powerful treatment is available when you ivermectin for dogs dosage chart from one of these pharmacies

  11. I always emailed this blog post page to all my friends,
    because if like to read it afterward my contacts will too.

  12. I am regular visitor, how are you everybody? This piece of writing posted at
    this website is really fastidious.

    https://w1.livesydney.co/

  13. ideal umzüge表示:

    ideal umzüge

    Hi there, just wanted to mention, I loved this article.
    It was practical. Keep on posting!

  14. LarryCoell表示:

    deneme bonusu veren siteler yerliarama.org deneme bonusu veren siteler betturkey betturkey.com deneme bonusu veren siteler yeni

  15. 10 Sites To Help You To Become A Proficient In Kia Sportage Key Fob 2013 Kia Rio
    Key Fob Programming (Angerway64.Werite.Net)

  16. You’ll Never Be Able To Figure Out This Panels For Upvc Doors’s Tricks Panels For Upvc Doors

  17. My programmer is trying to convince me to move
    to .net from PHP. I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using Movable-type on various websites for about a year and am worried about switching to another platform.
    I have heard excellent things about blogengine.net.
    Is there a way I can import all my wordpress content into it?
    Any kind of help would be greatly appreciated!

  18. Derekfar表示:

    slot oyunlar?: az parayla cok kazandiran slot oyunlar? – en kazancl? slot oyunlar?

  19. Uazraes表示:

    Официальная покупка аттестата о среднем образовании в Москве и других городах

    thisisyourreponguns.com

  20. Uazrvho表示:

    Как купить диплом о высшем образовании с минимальными рисками

    tanphuockhanhvillage.vn/kupit-attestat-za-10-i-11-klassi.html

  21. LarryCoell表示:

    ultrabet bonus ultrabet giris ultrabet yeni giris 1125

  22. prinz umzüge表示:

    prinz umzüge

    Hello, this weekend is pleasant designed for me, since this time i am reading this impressive informative article here at my residence.

  23. instagram anonymous story viewer storyinst.com .

發佈留言

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