数组操作-去除重复和空白元素

        /// <summary>
        /// 字符串数组转化为整形数组 并进行冒泡排序
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private int[] ArrToInt(string[] str)
        {
            int[] arr2 = new int[str.Length];   //用来存放将字符串转换成int[]
            for (int i = 0; i < str.Length; i++)
            {
                arr2[i] = int.Parse(str[i]);
            }
            int len = arr2.Length;
            for (int j = 0; j < len; ++j)
            {
                for (int i = j; i < len; ++i)
                {
                    if (arr2[j] > arr2[i])
                    {
                        int temp = arr2[j];
                        arr2[j] = arr2[i];
                        arr2[i] = temp;
                    }
                }
            }

            return arr2;
        }
        /// <summary>
        /// 重组数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string[] strTo(string [] str)
        {
            ArrayList al = new ArrayList();//去除数组中的重复元素
            for (int i = 0; i < str.Length; i++)
            {
                if (al.Contains(str[i]) == false && str[i] != "")
                {
                    al.Add(str[i]);
                }
            }

            //把ArrayList转换数组
            str = new string[al.Count];
            str = (string[])al.ToArray(typeof(string));
            return str;

        }
时间: 2024-10-24 11:11:28

数组操作-去除重复和空白元素的相关文章

[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 有序数组中

Jquery 2个数组,去除重复的项目

长数组(Positions_Check) 短数组(PositionTitlesParent_Check) for (var i = 0; i < PositionTitlesParent_Check.length; i++) {        for (var j = 0; j < Positions_Check.length; j++) {            if (PositionTitlesParent_Check[i] == Positions_Check[j]) {       

java 数组如何去除重复的数据?

java数组去重example: String[] str={"john","lily","lily","lucy"}; List<String> list=new ArrayList<String>(); for(int i=0;i<str.length;i++){ if(!list.contains(str[i])){ list.add(str[i]); } }

数组如何去除重复数据,只保留一条

var arr = [1,2,3,4,5,6,1,6,7,2]; var newArr = []; for(var i =0;i<arr.length-1;i++){ if(newArr.indexOf(arr[i]) == -1){ newArr.push(arr[i]); } }

JS获取对象某列转化为数组并去除重复

const data=[{EnumKey:1,EnumValue:'测试1'},{EnumKey:2,EnumValue:'测试2'},{EnumKey:1,EnumValue:'测试1'}] const plineArr = data.map(obj => { return obj.EnumValue }) const tt= [...new Set(plineArr)] 结果: tt:['测试1','测试2'] 原文地址:https://www.cnblogs.com/volts0302/p

javascript数组操作(创建、元素删除、数组的拷贝)

这篇文章主要介绍了javascript数组操作,包括创建.元素的访问.元素删除.数组的拷贝等操作,还有其它示例,需要的朋友可以参考下 1.数组的创建 复制代码 代码如下: var arrayObj = new Array(); //创建一个数组var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长度var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]); 创

二维数组去除重复值和array_unique函数

今天遇到了一个问题,就是从数据库中去除的数组为一个二维数组,现在就是想将二位数组进行去重,但是在php中,对于一个一维数组,我们可以直接使用php的系统函数array_unique,但是这个函数不能对多维数组进行去除重复,因此我需要自己写一个去除二维数组重复值的函数. 1 function array_unique_fb($array2D){ 2 3 foreach ($array2D as $v){ 4 $v=join(',',$v);//降维,也可以用implode,将一维数组转换为用逗号连

[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