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,227,885 Responses

  1. LarryCoell表示:

    ultrabet bonus ultrabet ultrabet

  2. you’re in point of fact a excellent webmaster. The website loading pace is amazing.

    It seems that you’re doing any distinctive trick.

    Also, The contents are masterpiece. you’ve done a
    excellent activity on this matter!

  3. Hi, I read your blogs like every week. Your humoristic style is witty, keep it up!

  4. ClydePab表示:

    ultrabet bonus: ultrabet – ultrabet giris

  5. Family members of Asbestos Legal workers too are
    at risk. Mesothelioma can take between 30 and 60 years to develop following
    exposure. Most cases appear in the pleura surrounding the lungs, whereas other
    forms of the disease are found in the peritoneum.

  6. Selena表示:

    The Most Common Mistakes People Do With Realistic Sex Dolls most realistic feeling sex
    doll (Selena)

  7. aiporn.men表示:

    Great blog here! Also your site loads up very fast!

    What web host are you using? Can I get your affiliate link to your host?
    I wish my website loaded up as quickly as yours lol

  8. LarryCoell表示:

    casino siteleri win guvenilir casino siteleri Casino Siteleri

  9. ClydePab表示:

    slot tr online: slot tr online – slot oyunlar? puf noktalar?

  10. The Top Reasons Why People Succeed Within The Male Masturbation Sex Toys Industry Masturbation Machine

  11. Тут можно преобрести купить сейф несгораемый противопожарный сейф

  12. What Experts In The Field Want You To Know?

    fiat key replacement

  13. porn表示:

    I aⅼl tthe time emaiⅼed this wweb site post page to all my friends,because
    if like to reаd іt after that my lіnkks will too.

    Μy blog post porn

  14. Five Sports Toto Lessons Learned From Professionals 스포츠토토

  15. Тут можно преобрести оружейный шкаф купить оружейные шкафы москва

  16. Тут можно преобрести огнестойкий сейф купить огнеупорные сейфы

  17. Derekfar表示:

    ultrabet tr online: ultrabet yeni giris 1125 – ultrabet tr online

  18. I do agree with all of the ideas you’ve presented for your post.

    They’re really convincing and can definitely work.
    Nonetheless, the posts are very short for newbies.
    Could you please prolong them a bit from next time? Thank you for the post.

  19. I could not refrain from commenting. Perfectly written!

  20. Wow! In the end I got a website from where I be capable of really take useful facts regarding my
    study and knowledge.

  21. Stephensoobe表示:

    Ни одно застолье невозможно представить себе без освежающих безалкогольных напитков. Сейчас вам не придется тратить свое время и силы на то, чтобы ездить в магазины и покупать соки, минеральную воду доставка алкоголя на дом москва 24алкосервис

  22. Guide To Treadmills That Fold Flat: The Intermediate Guide
    The Steps To Treadmills That Fold Flat treadmills that Fold flat

發佈留言

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