473,378 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

distinct selection(collection)

I have strings variables in a collection list and I want to create new
collection but to add to it only strings that are distinct (no common
strings).
For example I have an object sentense which is the base for a
collection and there are words in it and I want to create a new
collection of sentenses where there is no similar secound word in
those sentenses.
How do I do this distinct selection from a collection of object?
Thanks!
Nov 16 '05 #1
7 2694
You don't say what collection class you're using so I don't know all the
possibilities or limitations, but in your situation, I would probably create
a custom collection class, inherited from the ArrayList class, and add
methods to return arrays of string objects not having word x at position y.

DalePres
MCAD, MCSE, MCDBA

"juli" <ji****@gmail.com> wrote in message
news:1f*************************@posting.google.co m...
I have strings variables in a collection list and I want to create new
collection but to add to it only strings that are distinct (no common
strings).
For example I have an object sentense which is the base for a
collection and there are words in it and I want to create a new
collection of sentenses where there is no similar secound word in
those sentenses.
How do I do this distinct selection from a collection of object?
Thanks!

Nov 16 '05 #2
Hello ,
I am declaring a collection class which inherits from CollectionBase.
I have a string variable that is part of objects in collection.
How exactly I create a new collection choosing distinctly those strings?
(which method)
Thanks a lot!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Juli,

That would not difficult for you in my opinion.

Create a new collection, loop through the old one, add what you want in the
new record and write the record in the new collection as soon as the key is
different from the last one while looping. Write as well a row at the end
and skip the first time.

This is the oldest method of dataprocessing.

I hope this helps,

Cor
Nov 16 '05 #4
Hello,
I will try to define my problem better: I have a collection that is made
from a text file (long one) and it will take too much time to go through
all the collection.
I have there a string variable (one of collection object members) I want
to have a string array wich will contain all those string only once
(distinctly).How can I do it?
Thanks:)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Juli,

Did you try it, on an even .5Ghz computer I assume (did not test it now
however from expiriences) that it will probably take less time than about 1
millisecond to go through a collection from less than 10000 rows.

Cor
Nov 16 '05 #6
juli jul wrote:
Hello,
I will try to define my problem better: I have a collection that is made
from a text file (long one) and it will take too much time to go through
all the collection.
I have there a string variable (one of collection object members) I want
to have a string array wich will contain all those string only once
(distinctly).How can I do it?
Thanks:)

I reckon you want something like the Java Set interface. Below is a
simple implementation of a set I developed some time ago. This class
acts like an ArrayList, but it does not allow duplicate entries.

using System;
using System.Collections;

namespace Objectware.Collections {
public class Set : ICollection {
private ArrayList innerList=new ArrayList();
public Set() {}

public void CopyTo(Array array) {
CopyTo(array, 0);
}

public void CopyTo(Array array, int index) {
innerList.CopyTo(array, index);
}

public int Count {
get { return innerList.Count; }
}

public object SyncRoot {
get { return innerList.SyncRoot; }
}

public bool IsSynchronized {
get { return innerList.IsSynchronized; }
}

public int Add(object value) {
if (!Contains(value)) {
return innerList.Add(value);
} else return -1;
}
public void AddAll(ICollection collection) {
foreach (object item in collection) {
Add(item);
}
}
public void Remove(object value) {
innerList.Remove(value);
}
public void RemoveAll(ICollection collection)
{
foreach (object item in collection)
{
Remove(item);
}
}
public bool RetainAll(ICollection collection) {
ArrayList newList=new ArrayList(innerList);
foreach (object item in collection) {
newList.Remove(item);
}
int oldCount=innerList.Count;
innerList=newList;
return oldCount!=innerList.Count;
}

public bool Contains(object value) {
return innerList.Contains(value);
}
public bool ContainsAll(ICollection collection) {
foreach (object item in collection) {
if (!Contains(item)) return false;
}
return true;
}

public void Clear() {
innerList.Clear();
}

public IEnumerator GetEnumerator() {
return innerList.GetEnumerator();
}
}
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #7
Anders Norås wrote:

I reckon you want something like the Java Set interface. Below is a
simple implementation of a set I developed some time ago. This class
acts like an ArrayList, but it does not allow duplicate entries.


Note, that this is just as expensive as having an ArrayList directly.
Filling the list is worst-case and expected-random-input: O(n^2). Making
lookups is worst-case and expected-random-input O(n).

You could use something (roughly) as simple as:

public interface ISet: ICollection {
void Add(object item);
void Remove(object item);
bool Contains(object item);
}
public class HashSet: Hashtable, ISet {
void ISet.Add(object item) { base.Add(item,null); }
void ISet.Remove(object item) { base.Remove(item); }
bool ISet.Contains(object item) { return base.Contains(item); }
IEnumerator IEnumerable.GetEnumerator()
{ return base.Keys.GetEnumerator(); }
int ICollection.Count { get { return base.Count; } }
}

Which would give you O(n) filling and O(1) lookups.

ISet strings = new HashSet();
foreach ( string s in WhatEverReadsYourStrings )
try {
strings.Add(s)
} catch ( ArgumentException ) {
// Duplicate inserts are ignored
}
// strings now contains no duplicates

--
Helge
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: CJM | last post by:
I have a bit of code which involves some looping... In each iteration, we retrieve a string value... I want to build a list of DISTINCT (in the SQL sense) string values. Our example strings...
3
by: Marc L'Ecuyer | last post by:
Hi, I have a collection class derived from CollectionBase. This collection can add items of my class MyItem. In my class MyItem, I have a Selected property. When this property is set to true, I...
7
by: Jon Davis | last post by:
I'm managing "collections" / "lists" (??) of objects that are internally bound by an XML document, but I do NOT intend to offer an IEnumerator interface. For instance, with a root XML document,...
1
by: Dan H. | last post by:
Hello, I have an application that requires a collection that is sorted by a double field first, and then by a long field. So, lets say I have an class that has 2 fields called time, priority. I...
2
by: Matt Sawyer | last post by:
Hi, I'm attempting to do a drag and drop operation from one listbox to another. I have my listboxes setup with SelectionMode = MultiExtended so that I can use the shift key, cntrl key, etc. to...
3
by: Will Chamberlain | last post by:
I am running into a strange error when I try debugging my current application. A form loads up with a dynamically-populated textbox full of software titles. Upon selection of a software title and...
0
by: excelthoughts | last post by:
Hi, In VBA, you can select shapes and control them using Selection.ShapeRange. There is no equivalent in C#. See...
2
by: indhu | last post by:
Hi its not working, i want only distinct record. here its repeated records coming. accdb = "SELECT DISTINCT sequence FROM scene WHERE sceneid = '" & myquery & "' " and when i select the...
0
by: manwood | last post by:
I have a generic method that performs some basic sorting and removal of duplicates on some classes, but I cannot get the parameter to pass into the Distinct function working. This is my class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.