Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sending one array's members to another array by condition?

Sending one array's members to another array by condition?

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    omegazafer
    wrote on last edited by
    #1

    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.

    M A 2 Replies Last reply
    0
    • O omegazafer

      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.

      M Offline
      M Offline
      Mustafa Ismail Mustafa
      wrote on last edited by
      #2

      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?"

      O 1 Reply Last reply
      0
      • M Mustafa Ismail Mustafa

        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?"

        O Offline
        O Offline
        omegazafer
        wrote on last edited by
        #3

        Yes, you understood truely, I meant that.

        M 1 Reply Last reply
        0
        • O omegazafer

          Yes, you understood truely, I meant that.

          M Offline
          M Offline
          Mustafa Ismail Mustafa
          wrote on last edited by
          #4

          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?"

          O 1 Reply Last reply
          0
          • M Mustafa Ismail Mustafa

            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?"

            O Offline
            O Offline
            omegazafer
            wrote on last edited by
            #5

            Firs of all, I prefer array but if you have only solution on list. You can explain.

            M 1 Reply Last reply
            0
            • O omegazafer

              Firs of all, I prefer array but if you have only solution on list. You can explain.

              M Offline
              M Offline
              Mustafa Ismail Mustafa
              wrote on last edited by
              #6

              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?"

              1 Reply Last reply
              0
              • O omegazafer

                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.

                A Offline
                A Offline
                Anthony Mushrow
                wrote on last edited by
                #7

                //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.

                O 1 Reply Last reply
                0
                • A Anthony Mushrow

                  //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.

                  O Offline
                  O Offline
                  omegazafer
                  wrote on last edited by
                  #8

                  thanks a lot, for you responses.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups