-
-
Save mrsid96/352d2c1137d28ec4d7c02239bd1366ac to your computer and use it in GitHub Desktop.
temp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
const Duplicates = ({ numberList }) => { | |
let duplicates = [] | |
let uniques = [] | |
let hashMap = {} | |
numberList.forEach(element => { | |
if (hashMap[element]) { | |
hashMap[element] += 1; | |
} else { | |
hashMap[element] = 1; | |
} | |
}) | |
Object.keys(hashMap).forEach(element => { | |
if (hashMap[element] > 1) { | |
duplicates.push(element); | |
} else { | |
uniques.push(element); | |
} | |
}) | |
return ( | |
<> | |
Duplicates: {duplicates.join(", ")} <br/> | |
uniques: {uniques.join(", ")} | |
</> | |
) | |
} | |
export default Duplicates; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [numberList, setNumberList] = useState([]); | |
const submitNumberList = (e) => { | |
e.preventDefault(); | |
setNumberList(e?.target?.[0]?.value?.split(",")); | |
} | |
<h3>Duplicate numbers</h3> | |
<form onSubmit={submitNumberList}> | |
<input type="text" name="numberlist" placeholder="list of numbers: 1,2,3,4,5" /> | |
<input type="submit" value="Submit" /> | |
</form> | |
{ | |
numberList?.length !== 0 && | |
<Duplicates numberList={numberList} /> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment