Skip to content

Commit 599ee5d

Browse files
authored
Another solution for this exercise using Object
1 parent bbc47c3 commit 599ee5d

File tree

1 file changed

+12
-0
lines changed
  • JavaScript/chapter01/p01_is_unique

1 file changed

+12
-0
lines changed

JavaScript/chapter01/p01_is_unique/fvntr.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ describe(module.filename, () => {
1414
assert.deepEqual(isUnique([1,1,1,2,2,2,2,3,3,3,3]), [1,2,3]);
1515
});
1616
});
17+
18+
//Solution using Object
19+
const isUnique2=(arr)=>{
20+
let obj = {}
21+
for (let elem of arr){
22+
if(obj.hasOwnProperty(elem)) obj[elem]++
23+
else obj[elem] = 1
24+
}
25+
26+
return Object.keys(obj)
27+
}
28+
//isUnique2([1,1,1,2,2,2,2,3,3,3,3]) //returns[1,2,3]

0 commit comments

Comments
 (0)