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,235,279 Responses

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

  2. Derekfar表示:

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

  3. Coffee Machines From Bean To Cup Tools To Help You
    Manage Your Daily Life Coffee Machines From Bean To Cup Trick That Should
    Be Used By Everyone Be Able To coffee machines from bean to cup

  4. 10 Healthy Private Psychiatrist Durham Habits how much is
    a Private psychiatrist – daeshintravel.com

  5. The Top Asbestos And Peritoneal Mesothelioma Tricks To Transform Your Life mesothelioma lawsuits

  6. 11 “Faux Pas” That Are Actually Okay To Make With Your Case Opening Battles
    case Opening Battle csgo

  7. Abigail表示:

    Key Porsche Tips To Relax Your Daily Lifethe One Key Porsche Trick Every Person Should Learn key porsche – Abigail

  8. A Step-By Step Guide To Bi Fold Door Repair Near Me bifold door Repair

  9. Are You Tired Of Car Key Programmer? 10 Inspirational Sources To Bring Back Your Passion Cut and program car key (http://www.rw2828.com)

  10. We’re a group of volunteers and starting a new scheme in our community.
    Your site provided us with valuable info to work on. You’ve done a formidable job and
    our entire community will be grateful to you.

  11. Hello, Neat post. There’s an issue together with your web
    site in web explorer, could test this? IE still is the market chief
    and a large portion of folks will miss your magnificent writing due to this
    problem.

  12. ClydePab表示:

    deneme bonusu veren siteler betturkey: deneme bonusu veren yeni siteler – deneme bonusu veren siteler betturkey betturkey.com

  13. CUTWXER表示:

    Very shortly this website will be famous amid
    all blogging and site-building people, due to it’s fastidious articles or reviews

  14. ThePokies104表示:

    I enjoy what you guys tend to be up too. Such clever work and exposure!
    Keep up the wonderful works guys I’ve incorporated you guys to blogroll.

  15. CarrollUnton表示:

    deneme bonusu veren siteler denemebonusu2026.com https://slot-tr.online/# slot oyunlar?

  16. Hi there! I simply wish to offer you a huge thumbs up for the
    great info you’ve got right here on this post. I will be
    coming back to your web site for more soon.

  17. At this time I am ready to do my breakfast, later than having my
    breakfast coming again to read more news.

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

  19. Three Reasons Why Three Reasons Your Uk Private Psychiatrist Is Broken (And How
    To Fix It) Private Psychiatrist Near Me Uk

  20. I visited many web sites except the audio feature for audio songs
    present at this website is truly wonderful.

  21. WnrPaish表示:

    When you ivermectin 5 mg includes information. See the ED natural treatment options.|

  22. A Guide To Upvc Replacement Door Panels From Start To Finish Upvc Exterior Door Panels

  23. LarryCoell表示:

    Casino Siteleri Casino Siteleri guvenilir casino siteleri

  24. LLC表示:

    I am extremely impressed with your writing skills as well
    as with the layout on your blog. Is this a paid theme or
    did you modify it yourself? Anyway keep up the nice quality writing, it’s rare
    to see a great blog like this one today.

  25. CarrollUnton表示:

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

  26. ClydePab表示:

    ultrabet: ultrabet bonus – ultrabet

  27. Ricky表示:

    Five Killer Quora Answers To Private ADHD Assessment London private adhd
    assessment london – Ricky

發佈留言

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