简体   繁体   English

"对函数进行更改,该函数将序列作为参数并返回一个项目列表,其中没有任何具有相同值的元素"

[英]Changes to make for the function which takes as argument a sequence and returns a list of items without any elements with the same value

Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.实现函数 unique_in_order ,它接受一个序列作为参数并返回一个项目列表,其中没有任何相邻的具有相同值的元素并保留元素的原始顺序。 For example:例如:

uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B'] uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']

 var uniqueInOrder = function(it) { let n = 0; let arr = []; let i = 0; let m = 0; length = it.length; if (length == 0) { return arr } else { if (n == 0) { arr.push(it[0]); n += 1; } while (n < length) { if (arr[i] != it[m + 1]) { arr.push(it[m + 1]) i += 1; n += 1 } else { m += 1 n += 1 } } return arr } } console.log(uniqueInOrder('AAAABBBCCDAABBB'));

should reduce duplicates:应该减少重复:

Expected: '[\\'A\\', \\'B\\', \\'C\\', \\'D\\', \\'A\\', \\'B\\']'预期: '[\\'A\\', \\'B\\', \\'C\\', \\'D\\', \\'A\\', \\'B\\']'

instead got : '[\\'A\\', \\'B\\', \\'C\\', \\'D\\', \\'A\\']'而是得到'[\\'A\\', \\'B\\', \\'C\\', \\'D\\', \\'A\\']'

should treat lowercase as different from uppercase:应将小写字母视为与大写字母不同:

Expected: '[\\'A\\', \\'B\\', \\'C\\', \\'c\\', \\'A\\', \\'D\\']'预期: '[\\'A\\', \\'B\\', \\'C\\', \\'c\\', \\'A\\', \\'D\\']'

instead got : '[\\'A\\', \\'B\\', \\'C\\', \\'c\\']'而是得到'[\\'A\\', \\'B\\', \\'C\\', \\'c\\']'

You need to increase m at each iteration not just when a match is not found.您需要在每次迭代时增加m ,而不仅仅是在未找到匹配项时。 The same goes for nn

 var uniqueInOrder = function(it) { let n = 0; let arr = []; let i = 0; let m = 0; length = it.length; if (length == 0) { return arr } else { if (n == 0) { arr.push(it[0]); n += 1; } while (n < length) { if (arr[i] != it[m + 1]) { arr.push(it[m + 1]) i += 1; n += 1 } m += 1 n += 1 } return arr } } console.log(uniqueInOrder('AAAABBBCCDAABBB'));

you can do this :你可以这样做 :

 var uniqueInOrder = function(it){ var arr = it.split('') var newArr = [] arr.forEach((char,idx) => { if ( idx !== arr.length && char !== arr[idx+1]) newArr.push(char) }) return newArr }

The Solution can be implemented using该解决方案可以使用

  • Using rest & spread operator使用休息和传播运算符
  • Checking if the input is a string or an array检查输入是字符串还是数组

Below is the working code下面是工作代码

  var uniqueInOrder=function(iterable){
  if (typeof iterable === 'string') {
            let newArray = [];
            const [...arr] = [...iterable];
            for (let i = 0; i < arr.length; i++) {
                if (arr[i] != arr[i + 1]) {
                    newArray.push(arr[i])
                }
            }
             return newArray;
          }
  else{
    let newArray = [];
    for (let i = 0; i < iterable.length; i++) {
                if (iterable[i] != iterable[i + 1]) {
                    newArray.push(arr[i])
                }
            }
             return newArray;
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有可变数量参数的Javascript函数-更改其值-然后返回 - Javascript function that takes variable number of arguments - changes their value - and then returns them Function 以数字作为参数并返回 boolean javascript - Function that takes a number as an argument and returns a boolean javascript 一个返回 5 而不包含任何字母或数字的函数 - A function which returns 5 without having any alphabet or number in it 如果同一类的任何文本输入中的值发生变化,如何调用函数? - How to call function if the value changes in any text input with same class? 我必须创建一个函数,它接受一个字符串数组并返回一个数组,其中只有不以元音开头的字符串 - I have to make a function which takes an array of strings and returns an array with only strings that doesn't start with a vowel Function 接受输入文本,检查它是否具有某些元素,并返回 output 值 (JS) - Function that takes an input text, checks if it has certain elements, and returns an output value (JS) Javascript:接收对象的函数,返回object中描述的所有数字的列表 - Javascript: function which takes in object, and returns list of all numbers described in object 如何制作一个在 EventListener 设置变量后返回值的函数? - How to make a function which returns a value after a variable is set by an EventListener? 尝试制作一个节点 class function ,它接受一组项目并将它们全部推送到链表 - Trying to make a Node class function that takes an array of items and pushes them all to a linked list 接受通用参数并返回通用值的函数 - Function that takes generic arguments and returns generic value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM