简体   繁体   中英

Filter out all strings from a list that are a substring of another string in an array

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.

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