Forum > General

Removing duplicates from an array

<< < (7/7)

Thaddy:
I am never bloating. You can't read. And where appropiate I always provide a compilable example.
IOW you are fired! in the unlikely case you ever worked for me. I know a rat compared to a rabbit.

Shut this.

SymbolicFrank:
Turning them into indexed hashes and sorting those sounds good to me. Depending on how you sort, comparing them is often the thing that takes the most time and with hashes you only have to read the data once.
Then again, if the entries are long sentences, finding the first difference might be faster. I can think of edge cases either way.

Joanna:
I understand that knowing how to remove duplicates from an array has academic merit but in real life programming wouldn’t it be better to work on the problem of duplicate values getting into the array to begin with? It seems like no matter what you do it’s going to take a lot of extra time and effort to undo the duplicates in array.
Has there ever been considered an Function indexof(Avalue) to detect duplicates of an array or maybe a flag to not allow duplicates? If seen this for various other things so it seems like it should be possible to implement them for arrays.

Ten_Mile_Hike:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Wow, Hash tables, Log n ( time analysis), collections, helpers, generics, sorting procedures...You guys are way over my level of ability. Nevertheless; here is how I would detect andeliminate duplicates in an array and YES; my way is probably inefficient in both memoryand processor utilization, but it is just my way.  =============pseudo code=================var  Array_dups[0..9] of integer=[8,6,9,11,6,22,3,8,6];  Array_Fixed[0..9999] of integer; {Declare *expected* max range}  x:integer; Begin   Initialize_All_Elements_to_Zero (array_Fixed);   for x:=0 to High(Array_Dups) Do     Array_Fixed[Array_Dups[x]]            :=  Array_Fixed[Array_Dups[x]]+1; ////////////////////////////////////////////////////////////////////////////// At this point the elements of Array_fixed indicate the following         all elements  =0 did not occur in Array_Dups         all elements  =1 occurred only once in Array_Dups         all elements  >1 had duplicates in Array_Dups  It is now trivial to assign the values of Array_Fixed back into Array_Dups or manipulate or assign them in any manner that you choose   

Navigation

[0] Message Index

[*] Previous page

Go to full version