const returnUniquesArr = (arr) => [...new Set(arr)] function Component() { const prepareInputArr = (str) => str.split(' ') const [inputValState, setInputValState] = React.useState('a a b b c c') const [outputState, setOutputState] = React.useState(JSON.stringify(returnUniquesArr(prepareInputArr(inputValState.trim())))) const updateInput = (e) => setInputValState(e.target.value) const onChangeHandler = (e) => { updateInput(e) const str = e.target.value.trim() const inputArr = prepareInputArr(str) const uniquesArr = returnUniquesArr(inputArr) setOutputState(JSON.stringify(uniquesArr)) } return ( <> <input type="text" value={inputValState} onChange={onChangeHandler} /> <div>Uniques: {outputState}</div> </> ) }