DEV Community 👩‍💻👨‍💻

ivkeMilioner
ivkeMilioner

Posted on

Counting phrase in string.

I found a way to find repetitive words and count that words in the text. But I can't find a way to find and count phrases in the text. Do you have any solution?

https://jsitor.com/nhY17bVsu

let str = `Tennis superstar Novak Djokovic has pulled out of the ATP Cup in Sydney, amid speculation about his vaccination status and whether he will defend his Australian Open title next month. World No.1 Novak Djokovic has withdrawn from the 2022 ATP Cup,” organisers said in a statement on Wednesday, ahead of the start of the tournament on Saturday.ennis superstar Novak Djokovic has pulled out of the ATP Cup in Sydney, amid speculation about his vaccination status and whether he will defend his Australian Open title next month. World No.1 Novak Djokovic has withdrawn from the 2022 ATP Cup,” organisers said in a statement on Wednesday, ahead of the start of the tournament on Saturday.`
let names = str.split(' ')

const count = names =>
  names.reduce((a, b) => ({
    ...a,
    [b]: (a[b] || 0) + 1
  }), {}) // don't forget to initialize the accumulator

const duplicates = dict =>
  Object.keys(dict).filter((a) => dict[a] > 1)

console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
console.log(duplicates(count(names))) // [ 'Nancy' ]


Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
benjioe profile image
Benjioe • Edited on

Split on dots ?

Make Your Github Profile Stand Out

Github is great, but have you considered how to make yours more attractive for potential employers or other visitors? Even non-tech ones like recruiters!

Take a couple of hours and show your best side as a person - and a programmer.