Thread: how to avoid duplicate entry to a list/array?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    how to avoid duplicate entry to a list/array?

    Hi,

    Let's say I have an array. Every time I want to input an element, I want to make sure there is no duplicate entry in the array/list.

    I think the simplest method is doing a linear search against the array everytime I input the value. However, if the list grows longer, this method will not be efficient.

    How to avoid duplicate entry to a list/array in an effective manner?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you keep the array sorted, you can really cut down on the time it takes to search the array.
    On the plus side, stdlib.h contains both qsort() and bsearch() to make this pretty easy.

    Or you could implement a hash function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    to implement hash table, if i m not mistaken, it will allows quick matching to data.
    However, when inserting a new entry, the hash value of the new entry has to be calculated. If the collision occurs, the insertion will takes quite sometime, correct?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Yes u are right, So to avoid collision too many time, there are many way to find the key value which will end up in good hashing function which reduces clusters. For your Reference

    ssharish2005
    Last edited by ssharish2005; 12-16-2006 at 02:36 PM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    Quote Originally Posted by janetsmith
    Hi,

    Let's say I have an array. Every time I want to input an element, I want to make sure there is no duplicate entry in the array/list.

    I think the simplest method is doing a linear search against the array everytime I input the value. However, if the list grows longer, this method will not be efficient.

    How to avoid duplicate entry to a list/array in an effective manner?
    You want a "set", which is an associative container. there are two implementations for associative containers.

    One is the self-balancing binary search tree, which is sorted and gives a O(log n) to most operations; requires you to be able to compare elements. This is implemented with the "set" class in C++.

    The other is a hash table, which is not sorted and gives an average O(1) and worst-cast O(n) to most operations; requires you to have a hash function for the type. This is implemented with the "hash_set" class in C++.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    thanks, i will take a look on it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 PM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Database entry - avoiding duplicate records
    By ChadJohnson in forum Tech Board
    Replies: 2
    Last Post: 02-04-2006, 12:43 AM
  4. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  5. duplicate detection algorithm
    By Gustaff in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 12:26 PM