473,388 Members | 1,215 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,388 software developers and data experts.

How to delete unique items in an array

Hi

I need to write a program and part it's functionality is to delete unique items in an array. That is, delete items in the array that only occur once.

I also need to delete the first occurrence of repeated items, and I'm pretty sure the solution to these two will go hand in hand.

Thanks

akadeco
Aug 25 '09 #1
6 9995
bvdet
2,851 Expert Mod 2GB
akadeco,

Are you asking for help with your assignment? We can't help until you show some effort to solve this yourself.

BV
Moderator
Aug 25 '09 #2
kudos
127 Expert 100+
(I guess the assignment is too small to be a school assignment)

Expand|Select|Wrap|Line Numbers
  1. a = [1,2,3,4,3,4,4,4,5,6]
  2. b = {}
  3. i = 0
  4. for c in range(len(a)):
  5.  if(b.has_key(a[i]) == False):
  6.   b[a[i]] = 1
  7.   a.remove(a[i])
  8.  else: 
  9.   i+=1
  10. print a
  11.  
-kudos


@akadeco
Aug 25 '09 #3
bvdet
2,851 Expert Mod 2GB
Here's another way that creates a new list.
Expand|Select|Wrap|Line Numbers
  1. >>> a = [1,2,3,4,3,4,4,4,5,6]
  2. >>> b = []
  3. >>> for j, item in enumerate(a):
  4. ...     if a.count(item) > 1 and j > a.index(item):
  5. ...         b.append(item)
  6. ...         
  7. >>> b
  8. [3, 4, 4, 4]
  9. >>> 
Aug 25 '09 #4
kudos
127 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. a = [1,2,3,4,3,4,4,4,5,6]
  2. b=[]
  3. for c in a:
  4.  if(a.count(c) >=2 and b.count(c) < a.count(c)-1):
  5.   b.append(c)
  6. print b 
  7.  
Can we avoid using the list 'b'?

-kudos
Aug 25 '09 #5
bvdet
2,851 Expert Mod 2GB
With a list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> a = [item for j, item in enumerate(a) if a.count(item) > 1 and j > a.index(item)]
  2. >>> a
  3. [3, 4, 4, 4]
  4. >>> 
Aug 25 '09 #6
@Kudos thank you so much that is exactly what I needed!

Thank you all for your swift replies
Aug 26 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Sims | last post by:
Hi, I have a table that give a unique ID to certain Articles. To give the ID i simply use the Auto count of the DB. But what happens when i delete a record? i am now missing an ID. How can i...
1
by: Colin Steadman | last post by:
I have a multi-dimensional array that I want to delete items from. To do this I display a form and the user clicks some tick boxes. Then on the delete page I check which tick boxes were...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
8
by: nescio | last post by:
hello, i have an array and i don't know the content of it, but i want only unique values. in php there is a function to do this, but how must i do this in javascript? i have tried a lot and...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
7
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
10
pezholio
by: pezholio | last post by:
Hi, I'm trying to put together a page that deletes records from a database based on if an item is unticked, I've got the existing items in an array (let's call it array1 for the sake of argument):...
3
by: mark4asp | last post by:
Stack of limited size containing unique items? Hi guys, How would I implement a stack of limited size containing unique items? For example. Suppose my stack has . I add 2 to it and it is now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.