Sending one array's members to another array by condition?
-
Hi Friends, I have an array whose members are below int[] sayilar=new int[10] sayilar[0]=2; sayilar[1]=3; sayilar[2]=5; sayilar[3]=3; sayilar[4]=2; sayilar[5]=2; sayilar[6]=5; sayilar[7]=3; sayilar[8]=7; sayilar[9]=8; I want to send these the members of sayilar array to sayilar2 array but one member can be sent to sayilar2 array only one time. For example, 5 is repeated two times in sayilar array but sending to sayilar2 array it must be only one time.
-
Hi Friends, I have an array whose members are below int[] sayilar=new int[10] sayilar[0]=2; sayilar[1]=3; sayilar[2]=5; sayilar[3]=3; sayilar[4]=2; sayilar[5]=2; sayilar[6]=5; sayilar[7]=3; sayilar[8]=7; sayilar[9]=8; I want to send these the members of sayilar array to sayilar2 array but one member can be sent to sayilar2 array only one time. For example, 5 is repeated two times in sayilar array but sending to sayilar2 array it must be only one time.
omegazafer wrote:
I want to send these the members of sayilar array to sayilar2 array but one member can be sent to sayilar2 array only one time. For example, 5 is repeated two times in sayilar array but sending to sayilar2 array it must be only one time.
That doesn't hold much meaning now does it? Did you mean that you wanted to copy only a single instance of the elements in sayilar to sayilar two? Please rephrase.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook "There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib "Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"
-
omegazafer wrote:
I want to send these the members of sayilar array to sayilar2 array but one member can be sent to sayilar2 array only one time. For example, 5 is repeated two times in sayilar array but sending to sayilar2 array it must be only one time.
That doesn't hold much meaning now does it? Did you mean that you wanted to copy only a single instance of the elements in sayilar to sayilar two? Please rephrase.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook "There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib "Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"
Yes, you understood truely, I meant that.
-
Yes, you understood truely, I meant that.
Are you required to use arrays? Could you use another alternative dynamic data structure like a List ?
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook "There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib "Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"
-
Are you required to use arrays? Could you use another alternative dynamic data structure like a List ?
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook "There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib "Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"
Firs of all, I prefer array but if you have only solution on list. You can explain.
-
Firs of all, I prefer array but if you have only solution on list. You can explain.
The problem with arrays is that they are static in length. Once declared they can't be grown or shrunk unless you re-declare them. So in the first problem (2 arrays) assuming you copy an element from the first array to the second array and then after you have made a copy you check to see if there are any repetitions you will encounter gaps as such: aray1: |0|1|2|3|2|5| array2: |0|1|2|3| |5| so unless you waste extra loops to filter out the gaps into a 3rd array, you can't do much. With a dynamic data structure such as a list, the following answers your problem:
//initialize the list array2 to contain all the values of the Array array1 array2 = new List(array1); int repitionCounter = 0; //loop through the available values in array1 for (int i = 0; i < array1.Length; i++) { for (int j = 0; j < array2.Count; j++ ) { if (array2[j] == array1[i]) { repitionCounter++; } if (repitionCounter > 1) { array2.Remove(array2[j]); --repitionCounter; } } repitionCounter = 0; }
or even better, since you should be taking advantage of the Class methods:array2 = new List(); //loop through the available values in array1 for (int i = 0; i < array1.Length; i++) { if (!array2.Contains(array1[i])) //if the element does not exist in the List... array2.Add(array1); //add it }
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook "There is no wealth like knowledge, no poverty like ignorance." Ali ibn Abi Talib "Animadvertistine, ubicumque stes, fumum recta in faciem ferri?"
-
Hi Friends, I have an array whose members are below int[] sayilar=new int[10] sayilar[0]=2; sayilar[1]=3; sayilar[2]=5; sayilar[3]=3; sayilar[4]=2; sayilar[5]=2; sayilar[6]=5; sayilar[7]=3; sayilar[8]=7; sayilar[9]=8; I want to send these the members of sayilar array to sayilar2 array but one member can be sent to sayilar2 array only one time. For example, 5 is repeated two times in sayilar array but sending to sayilar2 array it must be only one time.
//Create new array
int[] sayilarTwo = new int[10];//count will be used to keep track of how many values
//are in the new array
int count = 0;/* For each int in sayilar, check if the current value
is a repeat by comparing it to all previous values.
If there is no match, add this value to the new array*/
for(int i=0; i<10; i++) {bool match = false; for(int j=0; j<i; j++) { if(sayilar\[j\] == sayilar\[i\]) match = true; } if(!match) { sayilarTwo\[count\] = sayilar\[i\]; //Value added, increment count count++; }
}
I just bodged that togeather, it will probably work. There's also probably better ways to do it.
My current favourite word is: PIE! Good ol' pie, it's been a while.
-
//Create new array
int[] sayilarTwo = new int[10];//count will be used to keep track of how many values
//are in the new array
int count = 0;/* For each int in sayilar, check if the current value
is a repeat by comparing it to all previous values.
If there is no match, add this value to the new array*/
for(int i=0; i<10; i++) {bool match = false; for(int j=0; j<i; j++) { if(sayilar\[j\] == sayilar\[i\]) match = true; } if(!match) { sayilarTwo\[count\] = sayilar\[i\]; //Value added, increment count count++; }
}
I just bodged that togeather, it will probably work. There's also probably better ways to do it.
My current favourite word is: PIE! Good ol' pie, it's been a while.
thanks a lot, for you responses.