Thread: find and replace duplicate numbers in array

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    find and replace duplicate numbers in array

    Hi all!

    Im having problems trying to find duplicate numbers in an array.Im guessing i should use a for loop but for the life of me I cant figure it out!

    Im not in college and am learning C++ of the internet so bear with me.

    I have a feeling its something fairly obvious and that I'm just confusing myself.
    Needless to say I dont the code done for me-that defeats the purpose,

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    If you are willing to use another container, one approach is to insert the elements of the array into a std::set, and check if the insertion failed. If it did, you know that the element that you attempted to insert is a duplicate.

    If you want to do things in-place, another approach is to sort the array (e.g., using std::sort()), then make a single pass to detect consecutive duplicates (akin to what std::unique() does, except that std::unique() actually removes the duplicates).

    EDIT:
    Oh, actually, if you use my second suggestion, you can just use std::adjacent_find() instead of implementing the single pass yourself.

    EDIT #2:
    Oh wait, your title says "find and replace", even though your post says nothing about replacement. You want to replace the found duplicates with something?
    Last edited by laserlight; 02-17-2009 at 06:00 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    Thanks laserlight for quick reply.

    The array is filled with random numbers,and I found that some of them repeat.The array has 28 elements and I can get a for loop to count through each element but cant figure out how to use the for loop to check someArray[0] and then get it to check the other 27 elements of the array to make sure that it isnt duplicated.

    the programme isnt part of a project Im just trying to learn about arrays and the like by trying different things,seeing what works and what doesnt and why not,

    thanks again

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Cathalo
    The array has 28 elements and I can get a for loop to count through each element but cant figure out how to use the for loop to check someArray[0] and then get it to check the other 27 elements of the array to make sure that it isnt duplicated.
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A different alternative solution is to either generate the numbers from a given list, and once the entry has been used, draw another from the bucket - so any given number can only exist once in the list. This means having a bool array or that is initialized to "not taken". When a number is picked, you check if it's been taken already, if so, loop again. If it's not taken, set it as taken and store in your list.

    Or, if you want something like a card-game, you would generate all the cards, and then shuffle - there are described methods to "shuffle" things - try using google for that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    @matsp ah i see,so say i have 52 in 1 array,randomly pick element from that array and check that it isnt in the other array(if it isnt set it to True so that its allowed)?Will look into that more

    Code:
    ///fill all elements with random number////////////////
    	for( int a=0;a<27;a++){
    		int tempA=a;
    		randomNumbers[tempA]=(rand()%45)+1;}
    
    		
       //for loop to check+change duplicates
    
    
    	for(int elementNumber=0;elementNumber<27;elementNumber++){
    		for (int a=elementNumber+1;a<27;a++){
    
    		if (randomNumbers[elementNumber]==randomNumbers[a]){
    			randomNumbers[elementNumber]=(rand()%45)+1;}    }
    		}
    just noticed that the 2nd for loop only checks after the element of the main array but doesnt check the ones before hand.Any way even if I start at the first element when I change it to a different value the change is not passed back to the array..

    will try and get it sorted and get back to you


    thanks again to both of you's..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find and replace a string in a file
    By salzouby in forum C Programming
    Replies: 13
    Last Post: 09-14-2010, 08:55 AM
  2. Find and replace within a string?
    By clancyPC in forum C Programming
    Replies: 3
    Last Post: 03-20-2009, 07:28 AM
  3. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 06:42 PM
  4. Find and replace
    By BobDole1 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2003, 10:06 PM
  5. Find and Replace Text Function
    By mart_man00 in forum C Programming
    Replies: 45
    Last Post: 03-13-2003, 10:21 PM