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,130,746 Responses

  1. Если вы искали где отремонтировать сломаную технику, обратите внимание – профи услуги

  2. Five Killer Quora Answers To Situs Terpercaya situs Terpercaya

  3. 부산 유흥메카 연산동 룸알바에서 큰 돈 한번 만져 보실래요?

  4. togel sydney表示:

    10 Times You’ll Have To Know About Daftar Situs Togel togel sydney

  5. kra4.at表示:

    I do not even know how I ended up here, but I thought this post was great.
    I do not know who you are but definitely you’re
    going to a famous blogger if you aren’t already 😉 Cheers!

  6. Drstobeese表示:

    vet pharmacy online: propranolol online pharmacy – medicine online shopping

  7. Why Car Key Programmer Near Me Will Be Your Next Big Obsession? Auto key programmer

  8. Если вы искали где отремонтировать сломаную технику, обратите внимание – техпрофи

  9. I like it whenever people get together and share ideas. Great website, keep it
    up!

  10. Brucecow表示:

    ретрит для женщин https://ретриты.рф

  11. Drstobeese表示:

    h-e-b pharmacy: letrozole online pharmacy – omeprazole online pharmacy

  12. Live Casino表示:

    Paragraph writing is also a fun, if you be acquainted
    with after that you can write or else it is complex to write.

  13. Foldaway Electric Wheelchair Tools To Make Your Daily Life Foldaway Electric Wheelchair Trick Every Individual Should Know foldaway electric wheelchair

  14. Eltonwag表示:

    ретрит для женщин https://ретриты.рф

  15. homepage表示:

    When some one searches for his essential thing, therefore he/she desires to
    be available that in detail, so that thing is maintained
    over here.

  16. Could Programming Keys Be The Key For 2023’s
    Challenges? programming car keys

  17. Drstobeese表示:

    pharmacy in artane castle: online pharmacy certification – Nortriptyline

  18. Easydrorbix表示:

    plavix pharmacy assistance: cytotec online pharmacy – viagra kuwait pharmacy

  19. See What Erbs Palsy Attorney Tricks The Celebs Are Using erbs Palsy attorney

  20. MACAU188表示:

    Excellent way of describing, and good paragraph to obtain facts on the topic of my
    presentation topic, which i am going to present in institution of higher education.

  21. Brucecow表示:

    психологический ретрит https://ретриты.рф

  22. Wow, this article is pleasant, my younger sister is analyzing these kinds of things,
    therefore I am going to tell her.

  23. my link表示:

    Thanks in support of sharing such a good idea,
    post is pleasant, thats why i have read it fully

    Have a look at my page my link

  24. Easydrorbix表示:

    viagra pharmacy online: propecia at boots pharmacy – tricare pharmacy crestor

  25. Fuck表示:

    Fucked out of my knickers.
    One day I was travelling with my brother Artyom in the car.
    He’s a year older than me. We were following his friends to celebrate his driving licence.
    The party was going to be at the dacha. Artem told me to sit in the back seat when his three friends got in. I was sandwiched between two of them.

    Stop by my website :: Fuck

  26. With havin so much written content do you ever run into any issues of plagorism or
    copyright infringement? My site has a lot of completely unique content I’ve either written myself or outsourced but it seems a
    lot of it is popping it up all over the internet without my
    authorization. Do you know any solutions to help reduce
    content from being stolen? I’d truly appreciate it.

發佈留言

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