通过递归使数组中没有重复项

//数组中删除值相同的项,使得数组中没有重复的项
//数组自己的方法splice(a,b),在每次使用后会返回一个新的数组,因此必须使用递归得方法来使数组中相同的项删除,如果不这样就达不到这样的效果,有些项会被忽略掉
//比如下面这个例子,如果不使用递归,输出的将是1,2,3,4,5,6,7,1很明显第一项和最后一项是相同的,没有达到我们预期的效果
//使用递归就不存在这个问题了,输出将是1,2,3,4,5,6,7;

 1 var arrzfs = [1,2,2,3,4,4,1,3,5,6,7,7,1,1,1,1,1];
 2 (function()
 3 {
 4     for(var i = 0; i < arrzfs.length-1; i++)
 5     {
 6         for(var j= i+1; j < arrzfs.length; j++)
 7         {
 8             if(arrzfs[i] == arrzfs[j])
 9             {
10                 arrzfs.splice(j,1);
11                 arguments.callee();//调用自己
12             }
13         }
14     }
15 })();
16 (function()
17 {
18     for(var i = 0; i < arrzfs.length; i++)
19     {
20         cc.log(arrzfs[i]);
21     }
22 })();
时间: 2024-07-29 11:21:10

通过递归使数组中没有重复项的相关文章

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. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, 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

[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 = [

[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 第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: '[