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
CodePudding user response:
[I hope this will help you,
Firstly convert the given string to an array. To do that use string.split("")
.
Secondly, create an map which will store word as key and count as value.
Now iterate through the stringArray and store the current word to the map. And increase the count for the word each time the word is found.
Check the below link ][1]
let words = "I am not gonna live forever, but I wanna live while I am alive";
function countRepeatedWords(sentence) {
let words = sentence.split(" ");
let wordMap = {};
for (let i = 0; i < words.length; i ) {
let currentWordCount = wordMap[words[i]];
let count = currentWordCount ? currentWordCount : 0;
wordMap[words[i]] = count 1;
}
return wordMap;
}
console.log(countRepeatedWords(words));
CodePudding user response:
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:
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.
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. 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);
}
CodePudding user response:
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
CodePudding user response:
Your code is fine. something wrong with this array ['nsmg33de1','nsmg33de1','2211,','2211','1234','1234'] use ['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);
CodePudding user response:
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
Use the following if the words aren't in order:
const getRepeatedWordsCount = list => {
let wordDict = {};
let total = 0;
for (let word of list) {
total = wordDict[word] ? 1 : 0;
wordDict[word] = 1;
}
return total;
}
console.log(getRepeatedWordsCount(['nsmg33de1','2211','1234','nsmg33de1','1234', '2211'])) // -> 3