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

  1. I Ԁon’t even know how I ended սp һere, but Ӏ thоught this post
    wаs ɡood. I do not knoᴡ whօ you ɑrе but ϲertainly you
    are ɡoing to а famous bloogger іf yyou arе not аlready 😉 Cheers!

    Hегe іs mmy wweb site – situs judi slot

  2. Good article. I will be facing many of these issues as well..

    Visit Tramadol Source – https://rx2go.com/buy/tramadol/

  3. Medicine information for patients. Brand names.
    order nolvadex online
    Best information about medicament. Get here.

  4. Bookmarks表示:

    I most likely edited the entire publication 10
    different times.

    Look into my webpage :: Bookmarks

  5. DannyDop表示:

    我们的设备是谁:我们将帮助谁

    Vibromera开发的振动平衡和分析设备专为维护旋转设备最佳性能的广泛专家和行业而设计。

    这包括各种类型的机器和机构,从工业风扇、泵和压缩机到复杂的涡轮机械和汽车发动机。

    让我们确定用户和应用程序的主要类别,我们的设备将作为一个宝贵的助手。
    专用振动分析设备 包括振动计、振动分析仪、加速度计和软件工具。 这些设备协作提供用于检测不平衡和诊断机械问题的基本数据。

    生产和维修公司

    我们的主要用户是从事各种旋转机械的生产,维护和维修的企业。

    这些可以是专门从事泵、风扇、电动机、涡轮机和其他设备的制造商和服务中心。

    我们的设备允许您直接在现场进行精密动态平衡,这大大简化了维护过程,提高了生产过程的效率。

    汽车及航空业

    汽车和航空业的公司面临着确保发动机轴和转子高精度平衡的需要。 这对于提高它们的性能、减少磨损和增加它们的使用寿命是很重要的。 我们的设备提供了一个解决方案,允许您在生产阶段和维护过程中有效地执行这些任务。

  6. Why Nobody Cares About Double Glazed Window Repairs Double Glazed Window Repairs Near Me

  7. Jani表示:

    What’s The Job Market For Online Shopping Uk Groceries Professionals?
    online shopping uk groceries; Jani,

  8. Some people plant Based vape uk flavored pure nicotine

  9. Lydia表示:

    5 Qualities People Are Looking For In Every Upvc Window
    Repair Near Me upvc windows repairs near me; Lydia,

  10. Drugs information leaflet. Brand names.
    where to buy keflex price
    Everything what you want to know about medication. Get here.

  11. A Proactive Rant About Upvc Windows Near Me co.n.s.u.m.erb.b.ek@olderworkers.com.au/author/dwuqi21erus1-katykahn-co-uk/”>Upvc Window Repairs

  12. Fzxxjs.com表示:

    Wow that was odd. I just wrote an incredibly long comment but after I
    clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say great blog! http://Fzxxjs.com/comment/html/?104394.html

  13. This vaporizer has a number of one-of-a-kind attributes.

    Here is my web blog: plant based vape australia

  14. 24 Hours To Improving Car Accident Claim car Accident Lawsuits

  15. Bookmarks表示:

    Portable vaporizers use batteries as a source of power.

    Here is my homepage Bookmarks

  16. Bookmarks表示:

    NewULife Ranks. Invite to my NewUlife Review.

    Also visit my web site :: Bookmarks

  17. The IQ is the most effective dry herb plant based vape uk I have attempted.

  18. Bookmarks表示:

    In this instance, the life insurance policy firm’s loan.

    Stop by my web page: Bookmarks

  19. Hi, i think that i saw you visited my weblog so i came to “return the favor”.I am
    attempting to find things to improve my site!I suppose its ok to use some of your ideas!!

  20. Medicine prescribing information. Drug Class.
    where can i get cheap zoloft without a prescription
    Best trends of drugs. Read information here.

  21. Hi, I do believe this is an excellent web site. I stumbledupon it 😉 I
    am going to revisit yet again since i have book-marked it.
    Money and freedom is the best way to change, may you be rich and continue to help other people.

  22. Medicament prescribing information. What side effects?
    where to buy generic prozac without a prescription
    Everything about medicines. Read information here.

  23. plant based vape near me-based
    vapes have a host of potential advantages.

  24. Bookmarks表示:

    Plant-based vapes have a host of prospective advantages.

    My page :: Bookmarks

發佈留言

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