Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. //Method 1
  2. function removeDuplicates(arr) {
  3. let obj = {};
  4. let len = arr.length;
  5.  
  6. for (let index = 0; index < len; index++) {
  7. let val = arr[index];
  8. obj[val] = val;
  9. }
  10.  
  11. return Object.values(obj);
  12. }
  13.  
  14. console.log(removeDuplicates([21, 15, 0,-1, 2, 21, 15, 9, 7, 6]));
  15. //[0, 2, 6, 7, 9, 15, 21, -1]
  16.  
  17. //Method 2
  18. function removeDuplicates2(arr) {
  19. return [...new Set(arr)];
  20. }
  21.  
  22. console.log(removeDuplicates2([21, 15, 0,-1, 2, 21, 15, 9, 7, 6]));
  23. //[21, 15, 0, -1, 2, 9, 7, 6]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement