1

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

Siavash_Sk
  • 35
  • 5
  • 1
    console.log() is your friend. Add logs to see what is happening. Or add a break point and walk through the code. `console.log(nums[i], nums[i+1], nums[i] === nums[i+1]); if(nums[i] === nums[i+1]){` That console.log line will show you why it is not working. Best skill to learn in development is debugging. – epascarello Dec 22 '21 at 20:16
  • 1
    That's not really the right algorithm for checking duplicates. You are only checking the item ahead in the array. So it wouldn't work if the strings in the array were all jumbled up. Also, I am guessing `nums[i+1]` is going to throw an error at the end of the array because you will go out of the index. – dj11223344 Dec 22 '21 at 20:16
  • 2 is the right output, you've just accidentally added a comma behind the first 2211. But yes like @dj11223344 said, if this were to be scrambled up it wouldn't really output anything useful. – jstnklnr Dec 22 '21 at 20:17
  • is there a better and easier function to find repeated string? – Siavash_Sk Dec 22 '21 at 20:20
  • Depends on your definition of better and easier. And it depends on what the actual requirements are. Do the pairs have to always be beside each other like yours is coded? – epascarello Dec 22 '21 at 20:22
  • @Siavash_Sk are you trying to find out how many duplicates there are, or what strings have duplicates? – dj11223344 Dec 22 '21 at 20:25
  • 1
    If duplicate anywhere in the array, plenty of answers here https://stackoverflow.com/questions/840781/get-all-non-unique-values-i-e-duplicate-more-than-one-occurrence-in-an-array – epascarello Dec 22 '21 at 20:25
  • exactly number of repeated arrays for example [12,13,14,12,14] ====== > 2 – Siavash_Sk Dec 22 '21 at 20:34

5 Answers5

4

[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));
Mirza Irtiza
  • 154
  • 7
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:

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);
}
jstnklnr
  • 84
  • 4
1

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

dj11223344
  • 113
  • 12
0

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
Vektor
  • 697
  • 5
  • 14
0

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);
Shawty
  • 59
  • 8