473,398 Members | 2,812 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,398 software developers and data experts.

duplicates in array

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

AK

May 21 '07 #1
14 12938
In article <11********************@u36g2000prd.googlegroups.c om>,
ak <aj**********@gmail.comwrote:
>Is it possible to find repeated(duplicate) 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...@gmail.comwrote:
Is it possible to find repeated(duplicate) 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*********************@y2g2000prf.googlegroups.c om>,
christian.bau <ch***********@cbau.wanadoo.co.ukwrote:
>On May 21, 6:47 pm, ak <ajinkya.c...@gmail.comwrote:
>Is it possible to find repeated(duplicate) 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
--
"Consideration 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...@gmail.comwrote:
Is it possible to find repeated(duplicate) 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...@gmail.comwrote:
Is it possible to find repeated(duplicate) 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...@gmail.comwrote:
Is it possible to find repeated(duplicate) 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...@gmail.comwrote:
On May 21, 10:47 pm, ak <ajinkya.c...@gmail.comwrote:
Is it possible to find repeated(duplicate) 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...@gmail.comwrote:
Is it possible to find repeated(duplicate) 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:
12345678910111213141516

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
In article <11**********************@p77g2000hsh.googlegroups .com>,
user923005 <dc*****@connx.comwrote:
>Is it possible to find repeated(duplicate) element in an array in
single loop ?
>Yes, if you use a hash table.
The implementation of the hash table is likely to use another loop.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 22 '07 #11
On May 22, 8:01 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <1179797653.904020.294...@p77g2000hsh.googlegroups .com>,

user923005 <dcor...@connx.comwrote:
Is it possible to find repeated(duplicate) element in an array in
single loop ?
Yes, if you use a hash table.

The implementation of the hash table is likely to use another loop.
Does it count if we unroll it?

May 22 '07 #12
Richard Tobin wrote:
user923005 <dc*****@connx.comwrote:
>>Is it possible to find repeated(duplicate) element in an array in
single loop ?
>Yes, if you use a hash table.

The implementation of the hash table is likely to use another loop.
Not so. For a std C implementation that expects insertion,
deletion, search to all be O(1) see:

<http://cbfalconer.home.att.net/download/hashlib.zip>

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 22 '07 #13
On 22 May 2007 14:20:22 -0700, user923005 <dc*****@connx.comwrote:
>On May 22, 8:01 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>In article <1179797653.904020.294...@p77g2000hsh.googlegroups .com>,

user923005 <dcor...@connx.comwrote:
>Is it possible to find repeated(duplicate) element in an array in
single loop ?
Yes, if you use a hash table.

The implementation of the hash table is likely to use another loop.

Does it count if we unroll it?
And you are going to do this, how?


May 22 '07 #14
In article <46***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>>Yes, if you use a hash table.
>The implementation of the hash table is likely to use another loop.
>Not so. For a std C implementation that expects insertion,
deletion, search to all be O(1) see:
Being O(1) has little to do with whether there's a loop. So long as
the expected number of items it compares against doesn't depend on N,
it's still O(1). I assume you mean that your hash table tries to
ensure a fixed maximum number of comparisons - perhaps one - in which
case it will have to adjust the table periodically, which is again
likely to use a loop. I'd be very surprised to see a hash table
implementation that never uses a loop.

Of course, you can always satisfy the letter of the law by using
recursion as a substitute for looping,

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 22 '07 #15

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

Similar topics

8
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...
3
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 ...
4
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
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...
2
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...
3
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...
2
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 =...
40
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. ...
3
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...
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...
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...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.