zoukankan      html  css  js  c++  java
  • 一道考察数组知识的编程题

    今天先贴代码,下次补上整体思路与后续优化

    题目:假设有一个集合 ['foo', 'bar', 'hello', 'world'],求这个集合里单词组合起来的所有不同的结果。

     1 let arr = ['foo', 'bar', 'hello', 'world'],
     2     str = '',
     3     newArr = [],
     4     results = [];
     5 
     6     for (let i = 0; i < 99; i++) {
     7         newArr.push(arr.sort(() => {
     8             return Math.random() > 0.5 ? 1 : -1;
     9         }).join(''));
    10 
    11     }
    12 
    13     unique = args => {
    14         for (let i = 0; i < args.length; i++) {
    15             if (results.indexOf(args[i]) < 0) {
    16                 results.push(args[i]);
    17             }
    18         }
    19         return results;
    20     }
    21 
    22     // 数组去重
    23     // newArr.unique();
    24     unique(newArr);
    25 
    26     console.log('the results is:
    ');
    27 
    28     results.map(item => {
    29         console.log(item);
    30     });

    粘到你的*.js文件中试试吧!

  • 相关阅读:
    HTML
    python io
    python 线程进程
    python socket
    python 面向对象2
    python 面向对象
    python hashlib模块
    python configparser模块
    python logging模块
    数组去重方法汇总
  • 原文地址:https://www.cnblogs.com/tim100/p/6815230.html
Copyright © 2011-2022 走看看