Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-06-2007, 04:49 PM   #1 (permalink)
~[::BOoYaKa::]~
 
Join Date: May 2005
Location: In a Cave.
Posts: 575
Unhappy Urgent help needed!!How to find Number of distinct elements in an array??


There is a problem I am stuck at, from yesterday. Using any loop(while, do...while, for), how to find the number of distinct elements in an array?
For example, if an array "a[5]" contains 1, 2, 1, 3, 2 then the number of distinct elements is 3. U can use if statement too.
The number of elements is known and stored in a variable "i".
Please someone help, its really urgent!!!
Thanx!
__________________
[::Eddie n Benoit Live Forever::]

Last edited by REY619; 09-06-2007 at 05:08 PM.
REY619 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 09-06-2007, 06:18 PM   #2 (permalink)
Alpha Geek
 
ashfame's Avatar
 
Join Date: Mar 2006
Location: Delhi / Jaipur
Posts: 765
Default Re: Urgent help needed!!How to find Number of distinct elements in an array??

Quote:
Originally Posted by REY619
There is a problem I am stuck at, from yesterday. Using any loop(while, do...while, for), how to find the number of distinct elements in an array?
For example, if an array "a[5]" contains 1, 2, 1, 3, 2 then the number of distinct elements is 3. U can use if statement too.
The number of elements is known and stored in a variable "i".
Please someone help, its really urgent!!!
Thanx!
Dude do dis (m givin da code for C / C++)((I donno any other language))
Code:
int arr[100]; (tak acc to ur requirements)
int count=0;
for (int i=1;i<100;i++)
{flag=1;
for (int j=i;j>=0;j--)
{
if (arr[j]==arr[i-1])
{ flag=0; }
if (flag==1)
{ counter++; }
}
}
I think dat wil do!
if ya face any problem, lemme knw
__________________
My RIG: C2D 2.0Ghz + Intel965 Mobo + OCZ Matched Pair 2X1GB DDR2 800Mhz + LG L226WTQ 22" LCD + CM 500W PSU + Palit HD 4850 512GDDR3
HOMEPAGE : http://www.ashfame.com | BLOG : http://blog.ashfame.com

Last edited by ashfame; 09-06-2007 at 06:26 PM.
ashfame is offline  
Old 09-06-2007, 06:58 PM   #3 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default Re: Urgent help needed!!How to find Number of distinct elements??

Or you could sort and store them and compare two consecutive elements only. While basic sort will require more computation than above algorithm, you can get faster results with efficient sorting algorithms...

Arun
sakumar79 is offline  
Old 09-06-2007, 07:29 PM   #4 (permalink)
Alpha Geek
 
ashfame's Avatar
 
Join Date: Mar 2006
Location: Delhi / Jaipur
Posts: 765
Default Re: Urgent help needed!!How to find Number of distinct elements??

Quote:
Originally Posted by sakumar79
Or you could sort and store them and compare two consecutive elements only. While basic sort will require more computation than above algorithm, you can get faster results with efficient sorting algorithms...
Arun
I agree that efficient sorting algorithms might perform better but what if the case is like holding values for a group in serial (for example: holding marks of students). In that case you cant even think of sorting the elements of array!

Anyways you also put forward a good logic!
Great!!
__________________
My RIG: C2D 2.0Ghz + Intel965 Mobo + OCZ Matched Pair 2X1GB DDR2 800Mhz + LG L226WTQ 22" LCD + CM 500W PSU + Palit HD 4850 512GDDR3
HOMEPAGE : http://www.ashfame.com | BLOG : http://blog.ashfame.com
ashfame is offline  
Old 09-06-2007, 07:44 PM   #5 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default Re: Urgent help needed!!How to find Number of distinct elements??

In case sorting the array will mess up the data you hold, store the sorted array in another variable and process that variable...

Arun
sakumar79 is offline  
Old 09-06-2007, 11:03 PM   #6 (permalink)
Alpha Geek
 
ashfame's Avatar
 
Join Date: Mar 2006
Location: Delhi / Jaipur
Posts: 765
Default Re: Urgent help needed!!How to find Number of distinct elements??

Quote:
Originally Posted by sakumar79
In case sorting the array will mess up the data you hold, store the sorted array in another variable and process that variable...
Arun
dude!
consider the memory limitations, just for a step you will create a new array!
thats nt gud!
__________________
My RIG: C2D 2.0Ghz + Intel965 Mobo + OCZ Matched Pair 2X1GB DDR2 800Mhz + LG L226WTQ 22" LCD + CM 500W PSU + Palit HD 4850 512GDDR3
HOMEPAGE : http://www.ashfame.com | BLOG : http://blog.ashfame.com
ashfame is offline  
Old 10-06-2007, 05:39 AM   #7 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default Re: Urgent help needed!!How to find Number of distinct elements??

It will depend on the actual requirement of the software... And the discretion of the programmer... whether to add to the memory requirement or to get slightly faster results...

Arun
sakumar79 is offline  
Old 10-06-2007, 07:10 PM   #8 (permalink)
~[::BOoYaKa::]~
 
Join Date: May 2005
Location: In a Cave.
Posts: 575
Default Re: Urgent help needed!!How to find Number of distinct elements??

THANKS a ton People!!
__________________
[::Eddie n Benoit Live Forever::]
REY619 is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Find Total Number of Websites? muralikrishnan QnA (read only) 5 21-06-2006 12:46 PM
Code to find Number of command buttons sid_b6505 QnA (read only) 3 06-12-2005 06:19 PM
Code to find number of command buttons sid_b6505 Tutorials 1 04-12-2005 09:34 PM
How to find phone number of a friend in yahoo messenger robogeek Mobiles and Tablets 2 17-11-2005 09:47 PM

 
Latest Threads
- by Who
- by Tenida
- by clinton
- by gohan89
- by icebags

Advertisement




All times are GMT +5.5. The time now is 08:22 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2