Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have an array that is sorted with insert sort:

 

public void InsertionSort()
	{
		int inner, outer;

		for(outer=1; outer<nElems; outer++)     // out is dividing line
		{
			long temp = a[outer];            // remove marked item
			inner = outer;                      // start shifts at out
			while(inner>0 && a[inner-1] >= temp) // until one is smaller,
			{
				a[inner] = a[inner-1];            // shift item to right
				--inner;                       // go left one position
			}
			a[inner] = temp;                  // insert marked item
		}  
	}

 

I want to remove any duplicates with NoDups().

The problem is that it must be an effective algorithm so I can't just shift down one space everytime a duplicate is found.

No element should be moved more than one time no matter how many duplicates there are in the array.

 

I would be really thankfull for any kind of tip

  • Administrators
Posted

How large an array are you looking at? It may be an alternative to copy the valid elements to a new array.

Alternatively could you not filter out the duplicates as you are adding them to prevent the duplicates in the first place.

also having a look under System.Collections may save some time as there is a SortedArray class that puts things into a sorted order and allows binary searches so finding duplicates would be quick. Or even just using System.Array.Sort could save you having to do your own sort routine.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...