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,703,919 Responses

  1. RogerFaf表示:

    https://fastpillseasy.com/# ed medications online

  2. Awesome content Thank you.

  3. Very quickly this site will be famous among all blogging users, due
    to it’s pleasant articles

    Also visit my blog post: Orion Depp Crypto Trading

  4. Azaliumbit表示:

    Hi there, I found your blog via Google whilst searching for a comparable subject,
    your web site came up, it looks great. I’ve bookmarked it in my google bookmarks.

    Hi there, just changed into aware of your blog via
    Google, and located that it’s truly informative.
    I’m going to watch out for brussels. I’ll be grateful when you proceed this in future.
    A lot of people might be benefited from your writing.
    Cheers!

  5. HoraceFak表示:

    https://2megagate.sbs – кракен kr2web in официальный

  6. I know this web site presents quality depending content and other data,
    is there any other site which gives these kinds of information in quality? http://Idesys.co.kr/bbs/board.php?bo_table=free&wr_id=250012

  7. Douglas Dunn表示:

    I’ve been considering a flat roof for my new extension, but I’m concerned about the installation process and maintenance Roof Repair Wilsonville, OR

  8. Greetings I am so glad I found your blog page, I really found you
    by mistake, while I was researching on Google for something
    else, Anyhow I am here now and would just like to say thanks a lot for
    a tremendous post and a all round exciting blog (I also love
    the theme/design), I don’t have time to look over it
    all at the minute but I have book-marked it and also included your RSS feeds, so when I have
    time I will be back to read a great deal more, Please do keep up
    the superb work.

    my page :: Orion Depp Crypto Advisor

  9. RogerFaf表示:

    https://maxpillsformen.com/# п»їcialis generic

  10. HoraceFak表示:

    https://2megagate.sbs – kraken сайт зеркала 2kraken click

  11. Donaldabula表示:

    Viagra without a doctor prescription Canada FastPillsForMen Viagra online price

  12. Wakelet.com表示:

    Loved this article! This topic really resonates with me.
    I never thought about it this way before, You made some excellent
    points, Thanks for shedding light on this issue; It’s
    interesting to see how different perspectives can shape our understanding.

    Your arguments are compelling and well-articulated. Overall,
    I think this topic is crucial for our community, and discussions like this help us grow and
    learn together. Thanks again for this valuable content!

    my blog post Press Release Chicago, Wakelet.com,

  13. HoraceFak表示:

    https://2megagate.sbs – kraken17 сайт зеркало

  14. Lela Brady表示:

    Can’t stress enough how important it is finding reputable businesses within this industry before making any commitments—do your research well ahead!!! #LongDistanceMovingCompany long distance movers queens

  15. RogerFaf表示:

    http://fastpillsformen.com/# buy viagra here

  16. Barrybib表示:

    Generic Viagra online: order viagra – buy Viagra over the counter

  17. HoraceFak表示:

    https://2megagate.sbs – kraken сайт анонимный

  18. My family all the time say that I am wasting my time here at net, except
    I know I am getting knowledge daily by reading thes pleasant content.

    my web site … Orion Depp YouTube

  19. BrettTIT表示:

    viagra without prescription: Fast Pills For Men – Generic Viagra online

  20. Mabel Simmons表示:

    Wonderful tips! Find more at رنگ ضد حریق

  21. Hey there! I could have sworn I’ve been to this site before but after reading
    through some of the post I realized it’s new to me.

    Anyhow, I’m definitely glad I found it and I’ll be book-marking and checking back often!

  22. Lloyd Cruz表示:

    The ocean is such a beautiful place to learn how to surf! Great insights can be found at kelly slater wave ranch

  23. RogerFaf表示:

    http://maxpillsformen.com/# Tadalafil price

  24. Charlesbot表示:

    http://maxpillsformen.com/# Buy Tadalafil 10mg

  25. HoraceFak表示:

    https://2megagate.sbs – kraken маркетплейс 18

  26. Ricardo表示:

    Responsible For A Best Sex Toys For Men Budget? 10 Unfortunate Ways To Spend Your Money adults toys for men – Ricardo

  27. HoraceFak表示:

    https://2megagate.sbs – https kraken13 актуальный

  28. Ada Stevenson表示:

    This was highly educational. More at بلفاروپلاستی چیست

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

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