简体   繁体   English

如何找到重复字符串的数量

[英]how to find the number of repeated strings

 const repeat= (nums) =>{ //* Done let ans = [] for(let i = 0; i< nums.length; i++){ if(nums[i] === nums[i+1]){ if(ans[ans.length -1].== nums[i]){ ans.push(nums[i]) } } } return ans.length } console,log(repeat(['nsmg33de1','nsmg33de1','2211,','2211','1234','1234']))

in this example it seems this function wont work properly there is 3 repeated string in the array but it will output 2在这个例子中,这个 function 似乎无法正常工作,数组中有 3 个重复的字符串,但它会 output 2

Depends on what value you want?取决于你想要什么价值? If you want the amount of duplications that have occurred, you could convert your list into a Set to remove all duplications and just calculate the difference of sizes between this set and the original list like this:如果您想要已经发生的重复数量,您可以将列表转换为 Set 以删除所有重复,并计算此集合与原始列表之间的大小差异,如下所示:

function repeat(values) {
  return values.length - new Set(values).size;
}

Otherwise if you want to know how many items there were which had at least 1 duplicate that would be a different story.否则,如果您想知道有多少项目至少有 1 个重复,那将是一个不同的故事。

For this you could potentially convert the set to an array and then map a 1 at every value where the value is found more than once in the array and a 0 for all the others.为此,您可以潜在地将集合转换为数组,然后 map 在数组中多次找到该值的每个值处为 1,而在所有其他值处为 0。 Afterwards you could reduce this array by adding all of these values together.之后,您可以通过将所有这些值相加来减少此数组。 Like this:像这样:

function repeat(values) {
  return [...new Set(values)].map(v => values.filter(o => o == v).length > 1 ? 1 : 0).reduce((a, b) => a + b);
}

I threw this together in the console.我把它放在控制台里。 Not the best algorithm in the world, but I think this is what you are trying to do:不是世界上最好的算法,但我认为这就是你想要做的:

const getCountOfDuplicates = (items) => {
   const count = {};
   
   items.forEach(item => {   
     if(count[item] && count[item] > 0) {
        count[item]++;
     } else {
        count[item] = 1;
     }
   });

    return Object.values(count).reduce((acc, value) => {
        if(value > 1) {
        acc++;
      }
      
      return acc;
    }, 0);
};

console.log('results 1', getCountOfDuplicates(['1','2','3','4','1','2','3','5', '6', '6', '7'])); // Output: "results 1", 4
console.log('results 2', getCountOfDuplicates(['nsmg33de1','nsmg33de1','2211,','2211','1234','1234'])) // Output: "results 2", 2

[I hope this will help you, Firstly convert the given string to an array. [我希望这会对你有所帮助,首先将给定的字符串转换为数组。 To do that use string.split("") .为此,请使用string.split("") Secondly, create an map which will store word as key and count as value.其次,创建一个 map,它将单词存储为键并计数为值。

Now iterate through the stringArray and store the current word to the map.现在遍历 stringArray 并将当前单词存储到 map。 And increase the count for the word each time the word is found.并在每次找到单词时增加单词的计数。

Check the below link ] 1检查以下链接] 1

Your code runs just fine.你的代码运行得很好。

Remove the comma , from the end of the 3rd item in the list.删除列表中第三项末尾的逗号,

['nsmg33de1','nsmg33de1','2211,','2211','1234','1234']

result:结果:

['nsmg33de1','nsmg33de1','2211','2211','1234','1234']

 const repeat = nums => { //* Done let ans = [] for (let i = 0; i < nums.length; i++) { if (nums[i] === nums[i+1]) { if (ans[ans.length-1].== nums[i]) { ans.push(nums[i]) } } } return ans.length } console,log(repeat(['nsmg33de1','nsmg33de1','2211','2211','1234','1234'])) // -> 3

Your code is fine.你的代码很好。 something wrong with this array ['nsmg33de1','nsmg33de1','2211,','2211','1234','1234'] use ['nsmg33de1','nsmg33de1','2211','2211','1234','1234']这个数组有问题 ['nsmg33de1','nsmg33de1','2211,','2211','1234','1234'] 使用 ['nsmg33de1','nsmg33de1','2211','2211',' 1234','1234']

and also try this raw code并尝试这个原始代码

const arr = ['nsmg33de1','nsmg33de1','2211','2211','1234','1234']

let obj = {};

arr.forEach(x => {
  obj[x] = (obj[x] || 0) + 1;
});

console.log(obj);

let valArray =Object.values(obj);

let conunt = 0;

valArray.forEach( c => {
    if(c > 1){
        conunt = conunt + 1;
    }
});

console.log(conunt);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM