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,450,211 Responses

  1. This was beautifully organized. Discover more at بلفاروپلاستی چیست

  2. CurtisBam表示:

    onlinecanadianpharmacy 24: canadian pharmacy near me – medication canadian pharmacy

  3. Brandonnot表示:

    canadian drug pharmacy: CanadianMdPharm – canadian drug pharmacy

  4. CurtisBam表示:

    Best Indian pharmacy: indian pharmacy – Indian Cert Pharm

  5. www.metooo.Io表示:

    10 Meetups On Robot Vacuum Deals You Should Attend automated Vacuum
    (http://www.metooo.Io)

  6. kypit 1s_acEn表示:

    программа 1с купить с установкой программа 1с купить с установкой .

  7. PAWG表示:

    Good information. Lucky me I ran across your website by accident (stumbleupon).
    I’ve bookmarked it for later!

  8. Carrolleruck表示:

    https://indiancertpharm.com/# Indian pharmacy international shipping
    canadian pharmacy ltd

  9. I’ve been exploring for a bit for any high quality articles or blog posts on this sort
    of house . Exploring in Yahoo I finally stumbled upon this web
    site. Reading this information So i am glad to express
    that I have a very excellent uncanny feeling I found out just what I
    needed. I most indisputably will make certain to
    do not overlook this website and provides it a glance regularly.

  10. kypit 1s_vxEn表示:

    1с бухгалтерия купить 1с бухгалтерия купить .

  11. The Top 20ft Shipping Container For Sale UK Experts Are Doing Three Things shipping Containers

  12. вывод из запоя в стационаре воронежа http://vyvod-iz-zapoya-v-stacionare-voronezh23.ru .

  13. CurtisBam表示:

    Best Indian pharmacy: Indian pharmacy international shipping – Online pharmacy

  14. kypit 1s_mqEn表示:

    программа 1с купить с установкой программа 1с купить с установкой .

  15. Shela表示:

    Why Karaoke Song Downloads Are Busy 대전유흥 (Shela)

  16. Brandonnot表示:

    Indian Cert Pharm: Indian pharmacy that ships to usa – indian pharmacy online

  17. Edwardrob表示:

    индивидуалка калуга калуга индивидуалка

  18. Orcbet表示:

    The Latest: Phil Mickelson to repay $1 million in SEC case
    By ASSOCIATED PRESS

    Published: 00:58 GMT, 20 May 2016 | Updated: 00:58 GMT, 20
    May 2016

    e-mail

    NEW YORK (AP) — The Latest on an SEC insider trading case
    involving golfer Phil Mickelson (all times local):

    7:45 p.m.

    A Las Vegas gambler accused of orchestrating an insider
    trading scheme involving pro golfer Phil Mickelson posted a $1 million bond
    to be freed from federal custody in Nevada ahead of a June 1 court date in U.S.
    District Court in New York.

    A lawyer for William “Billy” Walters presented a $100,000 check in court to secure the bond, and a U.S.
    magistrate judge ordered Walters to surrender his passport and weapons pending trial on 10 conspiracy
    and fraud charges.

    The 69-year-old Walters could spend the rest of
    his life in prison and have to surrender the proceeds of the alleged
    scheme. Prosecutors say he passed inside stock information between a director
    at a publicly traded food business and Mickelson.

    Former Dean Foods Co. board member Thomas Davis pleaded guilty this week in the case and promised to
    cooperate in the investigation.

    ___

    12:25 p.m.

    Professional golfer Phil Mickelson has agreed to forfeit nearly $1 million dollars that the Securities And Exchange Commission says he
    earned in an illegal insider trading scheme.

    The SEC says a gambler named William Walters received tips and business information about Dean Foods Co.
    from former Dean Foods director Thomas Davis between 2008 and 2012.

    Authorities say one of those tips was passed along to Mickelson, who made a big, successful stock trade with the information.

    Mickelson’s management group issued a statement Thursday
    saying that he had reached an agreement with the SEC to return all of
    his profit.

    The statement says Mickelson “has no desire to benefit from any transaction that the SEC sees as questionable.”

    ___

    10:51 a.m.

    The Securities And Exchange Commission is filing a complaint against professional golfer Phil
    Mickelson related to insider trading.

    The SEC says a gambler named William Walters received tips and business information about Dean Foods Co.
    from former Dean Foods director Thomas Davis between 2008 and 2012.

    In 2012, the SEC says Walters called Mickelson, who owed him money,
    and urged him to trade Dean Foods stock. The SEC says Mickelson did so the next day and made a profit of $931,000.

    Mickelson was not in the field of the Byron Nelson Classic in Irving,
    Texas, where play began Thursday morning. Calls to his representatives were not immediately returned.

  19. вывод из запоя в стационаре воронежа https://www.vyvod-iz-zapoya-v-stacionare-voronezh23.ru .

  20. jerk表示:

    What’s up, after reading this awesome post i am as well
    delighted to share my knowledge here with friends.

  21. CurtisBam表示:

    Mexican Easy Pharm: Mexican Easy Pharm – mexican rx online

  22. CurtisBam表示:

    Mexican Easy Pharm: Mexican Easy Pharm – Mexican Easy Pharm

  23. I every time used to read paragraph in news papers but now as I am a user of web
    so from now I am using net for posts, thanks to web.

發佈留言

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