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);