forked from ilife5/lifesinger.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ac87b3
commit 6ef53a8
Showing
3 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>unique</title> | ||
</head> | ||
<body> | ||
<script> | ||
|
||
function unique1(arr) { | ||
var ret = [] | ||
|
||
for (var i = 0; i < arr.length; i++) { | ||
var item = arr[i] | ||
if (ret.indexOf(item) === -1) { | ||
ret.push(item) | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
|
||
var indexOf = [].indexOf ? | ||
function(arr, item) { | ||
return arr.indexOf(item) | ||
} : | ||
function indexOf(arr, item) { | ||
for (var i = 0; i < arr.length; i++) { | ||
if (arr[i] === item) { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
function unique2(arr) { | ||
var ret = [] | ||
|
||
for (var i = 0; i < arr.length; i++) { | ||
var item = arr[i] | ||
if (indexOf(ret, item) === -1) { | ||
ret.push(item) | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
|
||
function unique3(arr) { | ||
var ret = [] | ||
var hash = {} | ||
|
||
for (var i = 0; i < arr.length; i++) { | ||
var item = arr[i] | ||
var key = typeof(item) + item | ||
if (hash[key] !== 1) { | ||
ret.push(item) | ||
hash[key] = 1 | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
|
||
var arr = ['a', 'b', 'c', '1', 0, 'c', 1, '', 1, 0, new Number(2), new String(2), 2, '2'] | ||
|
||
console.log(unique1(arr)) | ||
console.log(unique2(arr)) | ||
console.log(unique3(arr)) | ||
|
||
</script> | ||
</body> | ||
</html> |