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,232,688 Responses

  1. I do not know if it’s just me or if everybody else encountering problems with your blog.
    It appears like some of the text within your posts are running off the screen. Can somebody else please comment and let me know
    if this is happening to them as well? This could be a problem with my browser because I’ve had
    this happen previously. Thanks

  2. Ten Adult Adhd Assessment Myths You Shouldn’t Share On Twitter assessment adhd

  3. сколько стоит узаконить перепланировку квартиры http://www.stoimost-soglasovaniya-pereplanirovki-kvartiry.ru .

  4. LarryCoell表示:

    matadorbet bid matadorbet.bid matadorbet giris

  5. The 10 Most Dismal Treadmill Incline Failures Of All Time Could Have Been Prevented what Does treadmill incline mean

  6. Derekfar表示:

    Casino Siteleri: Canl? Casino Siteleri – Deneme Bonusu Veren Siteler

  7. Sazrqgr表示:

    Реально ли приобрести диплом стоматолога? Основные этапы
    x70795vj.beget.tech/2024/10/20/oficialnyy-diplom-legko.html

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

  9. Hello There. I found your blog using msn. This is a very well
    written article. I will be sure to bookmark it and
    come back to read more of your useful info. Thanks for the post.
    I’ll certainly comeback.

  10. Stephenideox表示:

    Погрузитесь на юдоль скорби веселий также увлекательных 3 сверху портале Любовь 28!
    инвестиции

  11. Derekfar表示:

    matadorbet bid: matadorbet bid – matadorbet giris

  12. Five Killer Quora Answers To Situs Togel Online situs Togel online – Weheardit.stream

  13. Why We Our Love For Item Upgrade (And You Should Also!) item level Upgrade

  14. Suzette表示:

    Find Out What Land Rover Discovery 4 Key Fob Replacement Tricks The Celebs Are Using land rover
    discovery key (Suzette)

  15. Derekfar表示:

    casino siteleri win: guvenilir casino siteleri – Casino Siteleri

  16. сколько стоит узаконить уже сделанную перепланировку https://stoimost-soglasovaniya-pereplanirovki-kvartiry.ru/ .

  17. сколько стоит узаконить перепланировку квартиры http://stoimost-soglasovaniya-pereplanirovki-kvartiry.ru/ .

  18. kra19.at表示:

    I’m gone to say to my little brother, that he should also go to see this blog on regular basis to obtain updated from most up-to-date information.

  19. Derekfar表示:

    Casino Siteleri: casino siteleri win – Casino Siteleri

  20. An Guide To Car Locksmith Near Me In 2023 car locksmith mobile

  21. Тут можно преобрести сейф для карабина купить сейф для оружия цены

發佈留言

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