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,627,278 Responses

  1. Использование текстов женского Интернет-периодики стопроцентная миров” рассматривается, но в разумных пределах, татуаж бровей.

    my website; https://remvip.ru/delaem-tatuazh-brovey-pravilno.html

  2. slot表示:

    Hi colleagues, how is the whole thing, and what you desire to say concerning this post,
    in my view its truly awesome for me.

  3. Keys Repair Near Me’s History Of Keys Repair Near Me In 10 Milestones
    keyless Entry Repair

  4. This is my first time visit at here and i am genuinely impressed to read all at alone place.

  5. togel4D表示:

    10 Things That Your Family Taught You About Togel4d togel4D

  6. LeroyBak表示:

    best india pharmacy Indian pharmacy to USA indian pharmacy online

  7. Jorgeagopy表示:

    Online medicine home delivery: Indian pharmacy online – online pharmacy india

  8. BrandonSaisy表示:

    Online medicine home delivery: indian pharmacy purchase online – best india pharmacy

  9. The Most Significant Issue With Mystery Box, And How You Can Fix It Mystery Box best

  10. Bryon表示:

    17 Reasons Why You Shouldn’t Ignore Adhd Assessment Uk cheap adhd assessment uk (Bryon)

  11. 5 Tools That Everyone Working Within The Retro American Fridge Freezer Industry Should Be
    Making Use Of retro american fridge Freezers

  12. 12 Stats About Fridge-Freezer To Make You Think Smarter About Other People fridge freezers freezers

  13. What’s The Job Market For Sofa Sets For Sale Professionals?
    sofa sets For Sale

  14. Jorgeagopy表示:

    ed drug comparison: cheap pharmacy online – buy anti biotics without prescription

  15. constantly i used to read smaller articles which as well clear their motive, and that is also
    happening with this article which I am reading at this place.

    https://virdsam.money/

  16. We are a group of volunteers and starting a new scheme in our community.
    Your web site provided us with valuable info to work
    on. You’ve done an impressive job and our whole community will be thankful to you.

  17. Why We Do We Love Fireplace On Wall (And You Should Also!) fireplace suites

  18. В нашем рейтинге Топ-10 лучших онлайн казино находится онлайн
    казино, которое стало лучшим в этих номинациях.

  19. SpectraX表示:

    We’re a group of volunteers and starting a new scheme in our community.
    Your web site offered us with valuable information to work on. You have done an impressive job and our entire community
    will be grateful to you.

  20. BrandonSaisy表示:

    Online medicine order: Pharmacies in India that ship to USA – cheapest online pharmacy india

  21. mesothelioma表示:

    The 10 Most Scariest Things About Mesothelioma Lawsuit mesothelioma

  22. BrandonSaisy表示:

    buying prescription drugs in mexico online: Legit online Mexican pharmacy – reputable mexican pharmacies online

  23. Williamagric表示:

    non prescription ed pills https://mexicanpharm24.cheap/# best online pharmacies in mexico

  24. Jorgeagopy表示:

    mexican drugstore online: mexico pharmacy – reputable mexican pharmacies online

  25. LeroyBak表示:

    erectile dysfunction medication low cost pharmacy impotence pills

  26. Upvc Door Hinges Tools To Streamline Your Life Everyday Upvc Window Hinge Repairs Near Me

  27. It is not my first time to visit this web page,
    i am browsing this site dailly and take nice data from here every day.

    Visit my page; 서울출장마사지

  28. Highly descriptive article, I loved that a lot. Will there
    be a part 2?

發佈回覆給「slot」的留言 取消回覆

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