From 599ee5de6fcc405a6b9f1c2351197c26020ad7d4 Mon Sep 17 00:00:00 2001 From: Ada-11 <52932540+Ada-11@users.noreply.github.com> Date: Fri, 30 Oct 2020 12:06:05 -0500 Subject: [PATCH] Another solution for this exercise using Object --- JavaScript/chapter01/p01_is_unique/fvntr.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/JavaScript/chapter01/p01_is_unique/fvntr.js b/JavaScript/chapter01/p01_is_unique/fvntr.js index ec83850a..45637aeb 100644 --- a/JavaScript/chapter01/p01_is_unique/fvntr.js +++ b/JavaScript/chapter01/p01_is_unique/fvntr.js @@ -14,3 +14,15 @@ describe(module.filename, () => { assert.deepEqual(isUnique([1,1,1,2,2,2,2,3,3,3,3]), [1,2,3]); }); }); + +//Solution using Object +const isUnique2=(arr)=>{ +let obj = {} +for (let elem of arr){ + if(obj.hasOwnProperty(elem)) obj[elem]++ + else obj[elem] = 1 +} + +return Object.keys(obj) +} +//isUnique2([1,1,1,2,2,2,2,3,3,3,3]) //returns[1,2,3]