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,371,165 Responses

  1. The records lesson blanketed with regards to natural carpets become attractive; thanks for enriching our mobile carpet cleaners

  2. I couldn’t resist commenting. Perfectly written!

  3. SamuelBix表示:

    prednisone 60 mg daily: Predni Best – prednisone price south africa

  4. I’m really enjoying the theme/design of your blog.
    Do you ever run into any web browser compatibility issues?
    A small number of my blog readers have complained about my site
    not operating correctly in Explorer but looks great in Safari.

    Do you have any recommendations to help fix this problem?

  5. KennethPah表示:

    prednisone 20mg tablets where to buy prednisone buy cheap buy prednisone canadian pharmacy

  6. Ola Moody表示:

    J’ai récemment utilisé vos conseils avant d’appeler mon serrurier local ici à Bordeaux! pena serrurerie

  7. Nikefut表示:

    free dark web darknet market dark market

  8. Very soon this site will be famous amid all blogging and site-building people,
    due to it’s pleasant posts

  9. KennethPah表示:

    canadian online pharmacy prednisone Predni Best prednisone cost 10mg

  10. I have been browsing online more than 3 hours
    today, yet I never found any interesting article
    like yours. It is pretty worth enough for me. In my opinion, if all webmasters and bloggers
    made good content as you did, the internet will be much more useful than ever
    before. https://365.Expresso.blog/question/la-clinique-sante-expert-a-st-louis-votre-partenaire-par-une-sante-optimale-18/

  11. How You Can Use A Weekly ADHD Diagnosis Project Can Change Your Life adhd diagnosis statistics

  12. SamuelBix表示:

    prednisone where can i buy: Predni Best – prednisone 30 mg daily

  13. 9 . What Your Parents Teach You About Male Mastubators male Mastubators

  14. KennethPah表示:

    cipro ciprofloxacin ciprofloxacin 500mg buy online where can i buy cipro online

  15. Case Battle The Process Isn’t As Hard As You Think Cs Case Battle

  16. Gavin Payne表示:

    My experience with my car accident injury lawyer showed me just how important it is to have someone knowledgeable on your side during tough times

  17. RobertJar表示:

    how to buy generic clomid: clomidonpharm – where to buy clomid tablets

  18. How To Create Successful Train Accident Attorney Strategies From Home Trucking Accident Lawyers Near Me

  19. SamuelBix表示:

    where can i buy clomid without insurance: buying generic clomid without insurance – where can i get clomid pill

  20. Nikefut表示:

    dark web market links darkmarket dark markets

  21. Cl-system.jp表示:

    Have you ever thought about adding a little bit more than just your articles?
    I mean, what you say is fundamental and everything.
    Nevertheless think about if you added some great visuals or video
    clips to give your posts more, “pop”! Your content is excellent
    but with pics and video clips, this blog could certainly be one of the very best in its niche.
    Wonderful blog! https://Cl-system.jp/question/catalogue-fiche-produit-3/

  22. I used to be recommended this blog via my cousin. I am now not positive whether this submit is
    written by way of him as nobody else recognize such exact approximately my problem.
    You’re incredible! Thanks! https://bdstarter.com/losteopathie-a-st-jean-quebec-une-approche-holistique-pour-votre-sante/

  23. DanielAbice表示:

    https://cipharmdelivery.com/# antibiotics cipro

  24. I can’t stress enough how helpful an accident attorney was for my good friend after their accident. If you require legal aid, think about going to Giddens Law Firm

發佈留言

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