Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
qiu-deqing committed Jul 6, 2014
1 parent 398dc09 commit f4dc6f3
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1050,5 +1050,55 @@ EventUtil.on(nav, 'click', function (event) {
});
</pre>

-
- **有一个大数组,var a = ['1', '2', '3', ...]; a的长度是100,内容填充随机整数的字符串。请先构造此数组a,然后设计一个算法将其内容去重**

<pre>
/**
* 数组去重
**/
function normalize(arr) {
if (arr && Array.isArray(arr)) {
var i, len, map = {};
for (i = arr.length; i >= 0; --i) {
if (arr[i] in map) {
arr.splice(i, 1);
} else {
map[arr[i]] = true;
}
}
}
return arr;
}

/**
* 用100个随机整数对应的字符串填充数组。
**/
function fillArray(arr, start, end) {
start = start == undefined ? 1 : start;
end = end == undefined ? 100 : end;

if (end &lt;= start) {
end = start + 100;
}

var width = end - start;
var i;
for (i = 100; i >= 1; --i) {
arr.push('' + (Math.floor(Math.random() * width) + start));
}
return arr;
}

var input = [];
fillArray(input, 1, 100);
input.sort(function (a, b) {
return a - b;
});
console.log(input);

normalize(input);
console.log(input);
</pre>


-

0 comments on commit f4dc6f3

Please sign in to comment.