[不断补充!!!]
1.找出数组中出现最多的元素和次数
测试用例:
let arr = [3, 5, 6, 5, 9, 8, 10, 5, 7, 7, 10, 7, 7, 7, 7, 10, 10, 10, 10, 10];
输出:70
解决方法:利用对象来统计,区别for...of 与 for...in 的使用
方法一:
function test(arr) {
if (!arr.length) return;
if (arr.length === 1) return arr[0];
let res = {};
let maxItem, maxCount = 0;
// 遍历数组
arr.forEach((item) => {
res[item] ? res[item] += 1 : res[item] = 1;
});
// 遍历 res
for (let i in res) {
if (res[i] > maxCount) {
maxCount = res[i];
maxItem = i;
}
}
return maxItem*maxCount;
}
console.log(test(arr));
方法二:
function test(arr){
let res ={};
arr.forEach(item=>{
if(res[item]){
res[item].count++;
}
else{
res[item] = {
key:item,
count :1
}
}
});
let count =0;
let item = 0;
res = Object.values(res);
for(let i of Object.keys(res)){
if(res[i].count >count){
count = res[i].count;
item = res[i].key;
}
}
return count*item;
}
let arr = [1,2,3,2,1,3,5,7,5,55,5,7];
test(arr);
- 把A集合中相同的元素统计出数量(基础)
测试用例:
var collection = [
"a", "a", "a",
"e", "e", "e", "e", "e", "e", "e",
"h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h",
"t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t",
"f", "f", "f", "f", "f", "f", "f", "f", "f",
"c", "c", "c", "c", "c", "c", "c", "c",
"g", "g", "g", "g", "g", "g", "g",
"b", "b", "b", "b", "b", "b",
"d", "d", "d", "d", "d"
];
it("把A集合中相同的元素统计出数量", function() {
var result = count_same_elements(collection);
expect(result).toEqual([
{key: "a", count: 3},
{key: "e", count: 7},
{key: "h", count: 11},
{key: "t", count: 20},
{key: "f", count: 9},
{key: "c", count: 8},
{key: "g", count: 7},
{key: "b", count: 6},
{key: "d", count: 5}
]);
解决方法:
function count_same_elements(collection) {
const obj = {};
collection.forEach(item => {
if(obj[item]) {
obj[item].count += 1;
} else {
obj[item] = {
key: item,
count: 1
}
}
});
return Object.keys(obj).map(item => obj[item]); //map 方法返回新数组
或
return Object.values(obj)
}
- 把A集合中相同的元素统计出数量(进阶)
测试用例:
var collection = [
"a", "a", "a",
"e", "e", "e", "e", "e", "e", "e",
"h", "h", "h", "h", "h", "h", "h", "h", "h", "h", "h",
"t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t",
"f", "f", "f", "f", "f", "f", "f", "f", "f",
"c", "c", "c", "c", "c", "c", "c", "c",
"g", "g", "g", "g", "g", "g", "g",
"b", "b", "b", "b", "b", "b",
"d-5"
];
it("把A集合中相同的元素统计出数量", function() {
var result = count_same_elements(collection);
expect(result).toEqual([
{key: "a", count: 3},
{key: "e", count: 7},
{key: "h", count: 11},
{key: "t", count: 20},
{key: "f", count: 9},
{key: "c", count: 8},
{key: "g", count: 7},
{key: "b", count: 6},
{key: "d", count: 5}
]);
解决方法:
function count_same_elements(collection) {
//在这里写入代码
let obj = {};
collection.forEach(item=>{
if(item.indexOf('-') !== -1){
let temp = item.slice(0,item.indexOf('-'));
obj[temp] = {
key:temp,
count:parseInt(item.slice(item.indexOf('-')+1)-1)
};
item = temp;
}
if(obj[item]){
obj[item].count++;
}
else {
obj[item]= {
key : item,
count :1
};
}
});
return Object.keys(obj).map(item => obj[item]);
}
- 删除偶数位重复元素
var collection_a = [1, 2, 3, 2, 5, 6, 21, 43, 12, 5];
var collection_b = [11, 11, 22, 11, 33, 11];
it('第偶数个元素中,选出不重复的元素', function() {
var result = single_element(collection_a);
expect(result).toEqual([6, 43, 5]);
});
it('第偶数个元素中,选出不重复的元素', function() {
var result = single_element(collection_b);
expect(result).toEqual([]);
});
解决方法:
var single_element = function(collection){
let res = collection.filter((item,index)=>{
return index%2 === 1;
});
let arr = [];
let flag = 0;
for(let i in res){
if(arr.includes(res[i])){
arr.splice(i,1);
flag = res[i];
}
else{
arr.push(res[i]);
}
}
for(let i in arr){
if(arr[i] === flag){
arr.splice(i,1);
}
}
return arr;
};
4.两大一小排序
it('两大一小排序', function() {
var result = rank_by_two_large_one_small(collection_a);
expect(result).toEqual([2, 3, 1, 6, 8, 4, 9, 10])
});
解决办法:
function rank_by_two_large_one_small(collection){
collection.sort(compare);
collection.forEach((val, index) => {
if (index % 3 === 0 && collection[index+2]) {
let value = parseInt(collection.splice(index, 1).toString()); //保存删除的值
collection.splice(index+2, 0, value); //在后移两位 插入
}
});
return collection;
}
function compare(a,b){
return a-b;
}
网友评论