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,479,518 Responses

  1. лечение наркозависимости в стационаре zarabotokdoma.creartuforo.com/viewtopic.php?id=11483 .

  2. It’s the best time to make some plans for the future and it’s time to be happy.

    I have read this post and if I could I wish to suggest you some interesting things or suggestions.
    Perhaps you could write next articles referring to this article.
    I want to read even more things about it!

  3. выведение из запоя воронеж стационар https://gaslo.ukrbb.net/viewtopic.php?f=13&t=3413 .

  4. Earl Ramirez表示:

    Very informative post! Looking to have some work done on my pool enclosure; thinking of calling Swimming Pool Screen Repair

  5. You said it nicely.!

  6. лечение наркозависимости в стационаре лечение наркозависимости в стационаре .

  7. It’s very easy to find out any matter on net as compared to books, as I found this piece of
    writing at this website.

  8. Також передбачені часове та просторове
    шумозаглушення, додавання зернистості, розмиття, імли та інших ефектів.

  9. nodeposit表示:

    Pretty section of content. I just stumbled upon your web
    site and in accession capital to assert that
    I get actually enjoyed account your blog posts. Any way I’ll be subscribing to your feeds and even I achievement you access consistently quickly.

  10. выведение из запоя воронеж стационар http://rio16.ukrbb.net/viewtopic.php?f=3&t=1120/ .

  11. sell your Van表示:

    Having read this I thought it was extremely enlightening.
    I appreciate you spending some time and effort to put this informative article together.
    I once again find myself spending a significant amount of time both reading
    and leaving comments. But so what, it was still worth it!

    Feel free to visit my webpage; sell your Van

  12. Nikefut表示:

    dark web search engine darknet marketplace dark market

  13. BernardMop表示:

    plavix best price [url=https://plavixclo.com/#]PlavixClo Best Price[/url] Plavix 75 mg price

  14. Pills information for patients. Cautions.
    where can i buy cheap thorazine online
    Some trends of drugs. Get here.

  15. Adeline Drake表示:

    search engine marketing is an important for visibility in contemporary marketplace SEO Agency

  16. Some truly interesting information, well written and
    broadly speaking user friendly.

  17. выведение из запоя воронеж стационар https://superjackson.ukrbb.net/viewtopic.php?f=28&t=9734/ .

  18. вывод из запоя в стационаре [url=https://justforum.bestforums.org/viewtopic.php?f=26&t=4753/]https://justforum.bestforums.org/viewtopic.php?f=26&t=4753/[/url] .

  19. Clicking Here表示:

    This paragraph will assist the internet visitors for creating new weblog or even a weblog from start to end.

發佈留言

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