Skip to content

Commit d8cbdee

Browse files
committed
remove duplicates from array
1 parent ff1a066 commit d8cbdee

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Implement a function to remove duplicates from an array and returning an array of only unique elements */
2+
3+
function uniqueArray(arr) {
4+
var hashMap = {};
5+
var uniqueArr = [];
6+
7+
for (var i = 0; i < arr.length; i++) {
8+
if (!hashMap.hasOwnProperty(arr[i])) {
9+
uniqueArr.push(arr[i]);
10+
}
11+
}
12+
return uniqueArr;
13+
}
14+
15+
// test cases
16+
var myArray = [1, 2, 3, 5, 1, 5, 9, 1, 2, 8];
17+
console.log(uniqueArray(myArray));

0 commit comments

Comments
 (0)