Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d8cbdee

Browse files
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)

AltStyle によって変換されたページ (->オリジナル) /