Urgent help needed!!How to find Number of distinct elements??

Status
Not open for further replies.

REY619

Ambassador of Buzz
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!
 
Last edited:

ashfame

Padawan
Re: Urgent help needed!!How to find Number of distinct elements in an array??

REY619 said:
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:D:D:D:D:D))
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
 
Last edited:

sakumar79

Technomancer
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
 

ashfame

Padawan
sakumar79 said:
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!!
 

sakumar79

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

Arun
 

ashfame

Padawan
sakumar79 said:
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!
 

sakumar79

Technomancer
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
 
Status
Not open for further replies.
Top Bottom