473,569 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

duplicates in array

ak
Is it possible to find repeated(duplic ate) element in an array in
single loop ?

AK

May 21 '07 #1
14 12952
In article <11************ ********@u36g20 00prd.googlegro ups.com>,
ak <aj**********@g mail.comwrote:
>Is it possible to find repeated(duplic ate) element in an array in
single loop ?
Is the array in sorted order? Are the duplicates adjacent? Will there
be exactly one element which is duplicated, or might there be
several?, the duplicates of all of which must be found? Are the
array elements integral or floating point? Is there a known minimum
and maximum bound on them? Is there a particular order that the
duplicates must be output in? Is it sufficient to output the element
and the number of duplicates, or must the locations of each be output?

If it is sufficient to output the duplicated elements and number of
duplicates, the answer is Yes, provided that enough memory is available.

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
May 21 '07 #2
On May 21, 6:47 pm, ak <ajinkya.c...@g mail.comwrote:
Is it possible to find repeated(duplic ate) element in an array in
single loop ?
Of course. As long as you don't mind if the loop has quite a lot of
iterations.

for (k = 0; k < n*n; ++k) {
i = k / n; j = k % n;
...
}

May 21 '07 #3
In article <11************ *********@y2g20 00prf.googlegro ups.com>,
christian.bau <ch***********@ cbau.wanadoo.co .ukwrote:
>On May 21, 6:47 pm, ak <ajinkya.c...@g mail.comwrote:
>Is it possible to find repeated(duplic ate) element in an array in
single loop ?
>Of course. As long as you don't mind if the loop has quite a lot of
iterations.
Ooh, very clever!

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 21 '07 #4
On May 21, 10:47 am, ak <ajinkya.c...@g mail.comwrote:
Is it possible to find repeated(duplic ate) element in an array in
single loop ?
Yes, if you use a hash table. It's not clear if an auxilliary data
structure is allowed.
Yes, if the array is sorted. It's not clear if we know anything about
the data.

Your question is vague.

May 22 '07 #5
On May 21, 10:47 pm, ak <ajinkya.c...@g mail.comwrote:
Is it possible to find repeated(duplic ate) element in an array in
single loop ?

AK
Yes u can using a single pass of a loop....

but it may use an extra array space and the integer range shoould be
specified...
consider the array NUM[1...n]
consdier all numbers in the array are within the range 1...k then we
have another array A from 1...k

then the loop would be...

for(i=0;i<n;i++ )
{

if(A[NUM[i]]==0)
{
A[NUM[i]]=1;
}
if(A[NUM[i]]==1)
{
printf("%d",NUM[i]);
}
}

this loop structure prints only the repeated elements in an array of
integers....

May 22 '07 #6
On May 21, 10:47 pm, ak <ajinkya.c...@g mail.comwrote:
Is it possible to find repeated(duplic ate) element in an array in
single loop ?

AK

Yes u can using a single pass of a loop....

but it may use an extra array space and the integer range shoould be
specified...
consider the array NUM[1...n]
consdier all numbers in the array are within the range 1...k then we
have another array A from 1...k

then the loop would be...

for(i=0;i<n;i++ )
{

if(A[NUM[i]]==0)
{
A[NUM[i]]=1;
}
if(A[NUM[i]]==1)
{
printf("%d",NUM[i]);
}
}

this loop structure prints only the repeated elements in an array of
integers....

May 22 '07 #7
On May 21, 8:47 pm, Ramesh3839 <prames...@gmai l.comwrote:
On May 21, 10:47 pm, ak <ajinkya.c...@g mail.comwrote:
Is it possible to find repeated(duplic ate) element in an array in
single loop ?
AK

Yes u can using a single pass of a loop....

but it may use an extra array space and the integer range shoould be
specified...
consider the array NUM[1...n]
consdier all numbers in the array are within the range 1...k then we
have another array A from 1...k

then the loop would be...

for(i=0;i<n;i++ )
{

if(A[NUM[i]]==0)
{
A[NUM[i]]=1;
}
if(A[NUM[i]]==1)
{
printf("%d",NUM[i]);
}

}

this loop structure prints only the repeated elements in an array of
integers....
With unsigned integer, on a 32 bit system your array NUM has 4 billion
elements (about 16 GB in size).
I suggest that my hash table notion is a bit more practical.

IMO-YMMV.

May 22 '07 #8
On May 22, 12:46 am, "christian. bau"
<christian....@ cbau.wanadoo.co .ukwrote:
On May 21, 6:47 pm, ak <ajinkya.c...@g mail.comwrote:
Is it possible to find repeated(duplic ate) element in an array in
single loop ?
is this a class room exercise?
Of course. As long as you don't mind if the loop has quite a lot of
iterations.

for (k = 0; k < n*n; ++k) {
i = k / n; j = k % n;
...

}
<OTHow about creating a binary search tree (duplicates collide)? </
OT>

Mohan

May 22 '07 #9
Ramesh3839 said:

<snip>
>
for(i=0;i<n;i++ )
{

if(A[NUM[i]]==0)
{
A[NUM[i]]=1;
}
if(A[NUM[i]]==1)
{
printf("%d",NUM[i]);
}
}

this loop structure prints only the repeated elements in an array of
integers....
Let's try it, shall we? And just for fun, let's try it on an array with
no repeats at all. So there should be no output, according to you.

#include <stdio.h>

#define n 16

int main(void)
{
int i = 0;
int A[n + 1] = { 0 };
int NUM[n] = { 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16 };

for(i = 0; i < n; i++)
{

if(A[NUM[i]] == 0)
{
A[NUM[i]] = 1;
}
if(A[NUM[i]] == 1)
{
printf("%d", NUM[i]);
}
}
putchar('\n');
return 0;
}

Output:
123456789101112 13141516

Oopsie!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 22 '07 #10

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

Similar topics

8
7803
by: Michelle | last post by:
hi, i have created an array from recordset containing user names eg. (davidp, davidp, evenf, patricka, rebeccah) which i have sorted in alphabetical order, but i need to identify duplicates in this array and the number of times it has been duplicated. can someone help?
3
2729
by: Quack Boy | last post by:
I'm new to MS Access (97) and I need for a query to find *near* matches (not exact duplicates) mainly to weed out duplicates that may contain similar erronious data. EG Table1 Name Company Town Alan ABC Alphaville Brian B inc. Beta City Calan ABC Alpaville
4
1658
by: Graham | last post by:
Can someone point me to an algorithm for sorting double precision values that is stable with duplicates? Thanks, Graham
4
13955
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String). for (int k=0; k<myLength; k++){ boolean isthere=false; for (int l=0; l<sizeres; l++){ if (res.equals(my)){
2
4139
by: tuananh87vn | last post by:
Hi ! can anyone help me with the following topic: Find All Duplicates in a List of Numbers - Array implementation - -InitializeTree() -AddNode() -Add into this programs one function which counts all duplicates by using a binary search tree implemented as an array (note: set the array size to 5,000,000 or...
3
1479
by: ashimk1 | last post by:
Hi All, I have an array that contains duplicates as well unique numbers. ex- (21, 33, 35, 21, 33, 70, 33, 35, 50) I need to arrange it in such a way that all the duplicates will come up first followed by unique numbers. Result for the given example should be: (21, 21, 33, 33, 35, 35, 70, 50)
2
2114
by: chemlight | last post by:
I'm trying to figure out how to order an array based on the number of duplicates, and then remove all duplicates. This is my code right now: foreach($acID as $searchterms){ $results = mysql_query(SELECT * FROM table WHERE field LIKE '%$searchterms%') while($row = mysql_fetch_array($results)){
40
17947
by: kylie991 | last post by:
Hi I am stuck on checking if a 2D array has duplicates. I have to write a function to check if a column has duplicates and then another function to check if the rows have duplicates from a file. How would I go about this... I have the array reading a file correctly is it just the check for duplicates that I am stuck on... :)
3
25057
Thekid
by: Thekid | last post by:
I'm trying to figure out a way to find if there are duplicates in an array. My idea was to take the array as 'a' and make a second array as 'b' and remove the duplicates from 'b' using 'set' and then compare a to b. If they're different then it will print out 'duplicates found'. The problem is that even after trying different arrays, some with...
0
7615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.