1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Keep Unique Vlaues Only!

Discussion in 'Programming' started by microcon, Dec 3, 2013.

  1. #1
    Hi,

    Let's assume that I have a text list/file contains the following values:

    a
    b
    b
    c
    c
    d
    Code (markup):
    Now I want the output to have only:

    a
    d
    Code (markup):
    So, only the values those have occurred once.

    How I do that?
     
    Last edited: Dec 3, 2013
    microcon, Dec 3, 2013 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    First you chose your language.
    Then, you use that language to do either a comparison by line, or put into array, or or or.
    PHP pseudocode would be something akin to
    read file, get contents, check if content matches next line, if it does, delete both, move along, if not, check next and so on.
    if you put this into an array, you can use array_unique() in PHP.
     
    PoPSiCLe, Dec 4, 2013 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    If you need it in PHP, it can be achieved using:

    
    // Array
    $array  = array ('a', 'b', 'b', 'c', 'c', 'd');
    // Count elements
    $array = array_count_values($array);
    // Get the elements that have a count of only 1
    $array = array_keys(array_filter($array, create_function('$a','return $a == 1;')));
    
    PHP:
     
    ThePHPMaster, Dec 5, 2013 IP
  4. ByteCoder

    ByteCoder Active Member

    Messages:
    239
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    65
    #4
    Pseudo code that should fit most languages with minor fixes:
    (Since writing from my phone itll be a little mess and inaccurate)

    List<string> uniqueList = new List...;
    String[] fileContents = File.ReadFile(...);
    foreach(string line in fileContents)
    {
    if(uniqueList.Keys.Contains(line))
    {
    uniqueList.Remove(line);
    }
    else
    {
    UniqueList.Add(line);
    }
    }
     
    ByteCoder, Dec 6, 2013 IP
  5. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #5
    <?php
    $filename = 'file.txt';
    
    // Open the file
    $fp = @fopen($filename, 'r');
    
    // Add each line to an array
    if ($fp) {
      $array = explode("\n", fread($fp, filesize($filename)));
    
    print_r(array_unique($array));
    }
    PHP:
     
    crivion, Dec 8, 2013 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    crivion: that won't do what the OP wanted.
     
    PoPSiCLe, Dec 8, 2013 IP