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,231,848 Responses

  1. fantastic issues altogether, you just received a new reader.
    What may you suggest in regards to your publish that you made a few days
    in the past? Any positive?

  2. situs bokep表示:

    Amazing! This blog looks just like my old one!
    It’s on a totally different subject but it has pretty much the same layout and design. Superb choice of colors!

  3. 16 Facebook Pages That You Must Follow For Which Coffee Machines Are Best Marketers Best Cappuccino Machine Uk – Minecraftcommand.Science,

  4. feet fetish表示:

    If you are going for most excellent contents
    like myself, just go to see this website every day since it offers feature contents, thanks

  5. Thanks on your marvelous posting! I seriously enjoyed
    reading it, you’re a great author.I will make certain to bookmark your blog and will come back from now on.
    I want to encourage you to continue your great writing, have
    a nice holiday weekend!

  6. I like the valuable information you provide in your
    articles. I’ll bookmark your blog and check again here frequently.
    I’m quite sure I’ll learn lots of new stuff right here!
    Good luck for the next!

  7. This is a topic which is near to my heart… Take care!
    Exactly where are your contact details though?

  8. Who’s The Top Expert In The World On Misted Double Glazed Units Cost?

    can you repair misted double glazing (ahmad-poole.Hubstack.net)

  9. Derekfar表示:

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

  10. HassanbuifS表示:

    https://denemebonusuverensiteler.top/# deneme bonusu veren siteler 2024
    deneme bonusu veren siteler betturkey betturkey.com

  11. Sazrxlg表示:

    Как приобрести аттестат о среднем образовании в Москве и других городах

    monstaluck.mn.co/posts/70000425

  12. I enjoy what you guys are up too. Such clever work and coverage!
    Keep up the amazing works guys I’ve added you guys to
    blogroll.

  13. Karin表示:

    9 . What Your Parents Taught You About Daftar Akun Togel Resmi daftar akun togel resmi (Karin)

  14. CarrollUnton表示:

    deneme bonusu veren siteler https://denemebonusuverensiteler.top/# deneme bonusu veren siteler mycbet.com

  15. HassanbuifS表示:

    http://denemebonusuverensiteler.top/# deneme bonusu veren siteler yeni
    deneme bonusu veren yeni siteler

  16. CarrollUnton表示:

    deneme bonusu veren siteler mycbet.com https://ultrabet-tr.online/# ultrabet yeni giris 1125

  17. The Three Greatest Moments In Mesothelioma Attorney Texas History Mesothelioma Lawsuit

  18. 3 Common Reasons Why Your Glass Replacement Isn’t Working (And How To Fix It) house glass replacement

  19. Sazrbbm表示:

    Как приобрести аттестат о среднем образовании в Москве и других городах

    zpu-journal.ru/forum/view_profile.php?UID=314175

  20. Medicament information for patients. What side effects can this medication cause?
    generic amoxicillin over the counter
    Actual what you want to know about meds. Get information now.

發佈留言

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