I have a list of strings and want to remove every string which is a substring of another string. I'm trying to avoid an O(N^2) solution.
If I have a list such as
list = ['c', 'ca', 'cat', 'cat', 'd', 'do', 'dog', 'do']
I would like to receive the list
['cat', 'dog']
const removeSubs = items => { const matches = text => test => text.== test && test.indexOf(text) > -1 const cut = text => items.filter(matches(text)).length === 0 return [...new Set(items,filter(cut))] } let list = ['c', 'ca', 'cat', 'cat', 'd', 'do', 'dog'. 'do'] console.log(removeSubs(list))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.