随机生成数组,并且去重复项

1:先随机生成一组数字,并插入数组中:

function randomArray(len, min, max) {
    if( len > (max - min +1) ){throw new Error(‘len > (max - min +1)‘)} //检查所设定数组是否超出预定总数,如果是则抛出异常
    var result = [];
    for( var i = 0; i < len; i++){
        var e = Math.floor(Math.random()*( max - min +1) + min);
        result.push(e);
    }
    return result;
}
console.dir( randomArray(6, 2, 7) );

2:第一步当中已经随机生成一组数组,但数组里面的数字有可能是重复的,所以如果要做到数组里面的数字不重复出现,就得实现数组去重复项:

function check(result, e){
    for( i = 0; i < result.length; i++){
        if( result[i] == e){ //遍历数组,查看数组里面的数字是否与正在生成的e相同,如果是,则返回true;
            return true;
        }
    }
    return false;

}

3:已经有检查数组中是否有重复项的方法了,但要把check函数在result.puse(e)前调用,所以完整代码如下:

function randomArray(len, min, max) {
    if( len > (max - min +1) ){throw new Error(‘len > (max - min +1)‘)} //检查所设定数组是否超出预定总数,如果是则抛出异常
    var result = [];
    for( var i = 0; i < len; i++){
        do{
            var e = Math.floor(Math.random()*( max - min +1) + min);
        } while( check(result, e) );

        result.push(e);
    }
    return result;
}

function check(result, e){
    for( i = 0; i < result.length; i++){
        if( result[i] == e){    //遍历数组,查看数组里面的数字是否与正在生成的e相同,如果是,则返回true;
            return true;
        }
    }
    return false;

}

console.dir( randomArray(6, 2, 7) );

还有其它数组去重方法,把使用过的记录下来当做个备份:

1:用hasOwnProperty。

hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象。不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员。

function check(result, e) {
    for (var i = 0; i < result.length; i++) {
        if( result.hasOwnProperty(e) ) {
            return true;
        }
    }
    return false;
}
时间: 2024-08-07 13:39:24

随机生成数组,并且去重复项的相关文章

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A = [

筛选数组内的重复项

//给数组添加方法del(),作用是筛选数组内的重复项 Array.prototype.del = function() { var a = {}, c = [], length = this.length; for (var i = 0; i < length; i++) { var b = this[i]; var d = (typeof b) + b; if (a[d] === undefined) { c.push(b); a[d] = 1; } } return c; }

[LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 这道题是之前那道Remove Duplicates from Sorted Array 有序数组中

Leetcode_删除排序数组中的重复项

Leetcode  删除排序数组中的重复项 题目: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用 额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2. 你不需要考虑数组中超出新长度后面的元素. 示例 2: 给定 nums = [0,0,1,1,1,2,2,3,3,4]

[leetcode] 26. 删除排序数组中的重复项

26. 删除排序数组中的重复项 一开始各种坐标变换把我绕晕了- -,恶心的水题 class Solution { public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; int j = 0; for (int i = 0; i < nums.length; i++) if (nums[i] != nums[j]) nums[++j] = nums[i]; return ++j; } } 原文地址:http

26. 删除排序数组中的重复项

26. 删除排序数组中的重复项 package com.test; import java.util.Arrays; public class Lesson026 { public static void main(String[] args) { int[] nums = {0,0,1,1,1,2,2,3,3,4}; int length = removeDuplicates(nums); System.out.println(length); } private static int rem

LeetCode:删除排序数组中的重复项||【80】

LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: 给定 nums = [1,1,1,2,2,3], 函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 . 你不需要考虑数组中超出新长度后面的元素. 示例 2: 给定 nums =

LeetCode 第26题 删除排序数组中的重复项

/*26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2. 你不需要考虑数组中超出新长度后面的元素.示例 2: 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回新的

Leecode刷题之旅-C语言/python-26.删除数组中的重复项

/* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/ * * algorithms * Easy (42.77%) * Total Accepted: 89.1K * Total Submissions: 208.1K * Testcase Example: '[