1. IOS 数组去重的几种方式
import java.util.*;
class BaiDu
{
public static void main(String[] args)
{
TreeSet<String> tr = new TreeSet<String>();
String[] s ={"11","22","22","33","33","33"};
System.out.print("====处理来前=======");
for(int i=0;i<s.length;i++){
System.out.print(s[i]+" ");
tr.add(s[i]);
}
String[] s2= new String[tr.size()];
System.out.println("=====处理后自======");
for(int i=0;i<s2.length;i++){
s2[i]=tr.pollFirst();//从TreeSet中取出元素重新赋给数组
System.out.print(s2[i]+" ");
}
}
}
2. 数组去重的意义
vararr=[1,1,2,2,2,3,3,4,5,1,2,3];
arr.sort();
varstr=arr.join(",");
str=str.replace(/([^,]+,)1+/g,"$1");
console.log(str);
3. C语言问题,数组去重问题,
int main ()
{
int array[100];
int count = 0;
int result = 0;
int index = 0;
int val = 0;
int i = 0;
printf( "请输入数组长度及其数组数据:
" );
while( 1 )
{
result = scanf ("%d" , &val);
if( count == 0 )
{
memset(array, 0, sizeof(int)*100 );
count = val;
if(count > 100 || count < 2 )
{
printf( "输入的数组长度必须大于2并且小于100!
" );
fflush(stdin);
count = 0;
continue;
}
}
else
{
if( val >= 100 || val < 0 )
{
printf( "输入的数组数据必须为小于100的正整数!
" );
fflush(stdin);
count = 0;
continue;
}
array[val] = 1;
index++;
if( result == 1)
{
if( index == count )
{
break;
}
}
}
}
printf( "排序后的数组长度及其数组数据:
" );
count = 0;
for( i = 0x00 ; i < 100 ; i ++ )
{
if(array[i] == 1 )
count++;
}
printf( "%d", count);
for( i = 0x00 ; i < 100 ; i ++ )
{
if(array[i] == 1 )
printf( " %d", i);
}
printf( "
");
system(" PAUSE");
}
4. json数组如何去重
代码如下:
/**
* 去重复_id项合并value值
* @param args
*/
public static JSONArray delRepeatIndexid(JSONArray array) {
JSONArray arrayTemp = new JSONArray();
int num = 0;
for(int i = 0;i < array.size();i++){
if(num==0){
arrayTemp.add(array.get(i));
}else{
int numJ = 0;
for(int j = 0;j < arrayTemp.size(); j++){
JSONObject newJsonObjectI = (JSONObject)array.get(i);
JSONObject newJsonObjectJ = (JSONObject)arrayTemp.get(j);
String index_idI = newJsonObjectI.get("index_id").toString();
String valueI = newJsonObjectI.get("value").toString();
String timeI = newJsonObjectI.get("time").toString();
String itemidI = newJsonObjectI.get("itemid").toString();
String index_idJ = newJsonObjectJ.get("index_id").toString();
String valueJ = newJsonObjectJ.get("value").toString();
if(index_idI.equals(index_idJ)){
int newValue = Integer.parseInt(valueI) + Integer.parseInt(valueJ);
arrayTemp.remove(j);
JSONObject newObject = new JSONObject();
newObject.put("index_id", index_idI);
newObject.put("itemid", itemidI);
newObject.put("time", timeI);
newObject.put("value", newValue);
arrayTemp.add(newObject);
break;
}
numJ++;
}
if(numJ-1 == arrayTemp.size()-1){
arrayTemp.add(array.get(i));
}
}
num++;
}
return arrayTemp;
}
5. 数组元素为对象的数组如何去重
1.构建一个新的数组存放结果
2.for循环中每次从原数组中取出一个元素版,用这个元素循环与结果数组权对比
3.若结果数组中没有该元素,则存到结果数组中
复制代码代码如下:
Array.prototype.unique1 = function(){
var res = [this[0]];
for(var i = 1; i < this.length; i++){
var repeat = false;
for(var j = 0; j < res.length; j++){
if(this[i] == res[j]){
repeat = true;
break;
}
}
if(!repeat){
6. C++ 怎么进行数组去重
这题应该用二叉树或者散列表做
#include <iostream>
#include <set>
#include <list>
using namespace std;
int main()
{
int a[]={2, 5, 3, 12, 3, 16, 5, 9, 5};
int a_length=sizeof(a)/sizeof(int);
set<int> s;
pair< set<int>::iterator, bool > p;
list<int> l;
for(int i=0;i<a_length;i++){
p=s.insert(a[i]);
if(p.second) l.push_back(a[i]);
}
cout<<"无输入顺序:";
for(set<int>::iterator iter=s.begin();iter!=s.end(); iter++)
cout<<*iter<<" ";
cout<<"\n记录输入顺序:";
for(list<int>::iterator iter=l.begin();iter!=l.end(); iter++)
cout<<*iter<<" ";
return 0;
}
无输入顺序:2 3 5 9 12 16
记录输入顺序:2 5 3 12 16 9
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
7. C语言数组去重算法
#include <stdio.h> #include <malloc.h> using namespace std ; bool isRepeat(int *arr,int len,int elem) { for(int i = 0 ; i < len ; i++) if(arr[i]==elem) return true ; return false ; } int* delRepeat(int *arr,int len) { int *tar = (int*)malloc(sizeof(int)) ; int temp ; unsigned char i ; unsigned char j = 0 ; tar[j] = arr[0] ; for(i=1;i<len;i++) { if(!(tar,j+1,arr[i])) { tar = (int*)realloc(tar,sizeof(int)*(j+2)) ; tar[++j] = arr[i] ; } } return tar ; } int main() { int a[5] = {1,2,2,3,3} ; int *b = delRepeat(a,5) ; return 0 ; }
8. 如何简单地进行数组去重
用二叉树或者散列表做
#include <iostream>
#include <set>
#include <list>
using namespace std;
int main()
{
int a[]={2, 5, 3, 12, 3, 16, 5, 9, 5};
int a_length=sizeof(a)/sizeof(int);
set<int> s;
pair< set<int>::iterator, bool > p;
list<int> l;
for(int i=0;i<a_length;i++){
p=s.insert(a[i]);
if(p.second) l.push_back(a[i]);
}
cout<<"无输入顺序:";
for(set<int>::iterator iter=s.begin();iter!=s.end(); iter++)
cout<<*iter<<" ";
cout<<"\n记录输入顺序:";
for(list<int>::iterator iter=l.begin();iter!=l.end(); iter++)
cout<<*iter<<" ";
return 0;
}
无输入顺序:2 3 5 9 12 16
记录输入顺序:2 5 3 12 16 9
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
9. 总结一下数组去重的几个方法
function clear(arr) {
// 1 如何获取数组中每一个元素出现的次数
var o = {}; // 1.1 记录数组中元素出现的次数
for (var i = 0; i < arr.length; i++) {
var item = arr[i]; // 数组中的每一个元素
// o[item] = 1;
// 1.2 判断o对象是否有当前遍历到的属性
if (o[item]) {
// 如果o[item] 存在,说明次数不为1
o[item]++;
} else {
// 如果o[item] 不存在,说明是第一次出现
o[item] = 1;
}
}
// console.log(o);
// 2 生成一个新的数组,存储不重复的元素
var newArray = [];
// 2.1 遍历对象o中的所有属性
for (var key in o) {
// 2.2 判断o对象中当前属性的值是否为 1 如果为1 说明不重复直接放到新数组中
if (o[key] === 1) {
newArray.push(key);
} else {
// o对象中当前属性 次数不为1 ,说明有重复的,如果有重复的话,只存储一次
// 判断当前的newArray数组中是否已经有该元素
if (newArray.indexOf(key) === -1) {
newArray.push(key);
}
}
}
return newArray;
}
var array = ['c', 'a', 'z', 'a', 'x', 'a'];
var newArray = clear(array);
console.log(newArray);