473,382 Members | 1,362 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,382 software developers and data experts.

Counting unique elements in an array

Hi guys,

I need to count the unique elements in an array. For example, if i have an array,
array[0,0,0,1,2,3,3]. The number of unique elements in the array is 3. I am sorry if this message has been posted before. Help is appreciated. Thanks in advance.

Regards,

AZ.
Jul 27 '07 #1
5 39973
JosAH
11,448 Expert 8TB
Hi guys,

I need to count the unique elements in an array. For example, if i have an array,
array[0,0,0,1,2,3,3]. The number of unique elements in the array is 3. I am sorry if this message has been posted before. Help is appreciated. Thanks in advance.

Regards,

AZ.
Is the array sorted, i.e. are the non-unique elements adjacent to eachother?
(that was a spoiler already, I'm sure).

kind regards,

Jos
Jul 27 '07 #2
Yes, the arrays are sorted. Thanks for your reply.

AZ.
Jul 27 '07 #3
seforo
60
Actually there are 4 unique elements in your example array, zero is also a number. Since the numbers are sorted then it is simple:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.   const int size = 7;
  6.   int array[size] = {0,0,0,1,2,3,3};
  7.   int unique = 1; //incase we have only one element; it is unique!
  8.   for(int i = 0; i < size -1 /*since we don't want to compare last element with junk*/; i++)
  9.     {
  10.      if(array[i]==array[i+1])
  11.        continue;
  12.      else
  13.        unique++;
  14.     }
  15.   cout<<"The number of unique elements is "<<unique<<endl;
  16.   return 0;
  17. }
  18.  
I hope I have not broken the laws of the scripts by giving you a fully code.
Jul 27 '07 #4
Actually there are 4 unique elements in your example array, zero is also a number. Since the numbers are sorted then it is simple:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.   const int size = 7;
  6.   int array[size] = {0,0,0,1,2,3,3};
  7.   int unique = 1; //incase we have only one element; it is unique!
  8.   for(int i = 0; i < size -1 /*since we don't want to compare last element with junk*/; i++)
  9.     {
  10.      if(array[i]==array[i+1])
  11.        continue;
  12.      else
  13.        unique++;
  14.     }
  15.   cout<<"The number of unique elements is "<<unique<<endl;
  16.   return 0;
  17. }
  18.  
I hope I have not broken the laws of the scripts by giving you a fully code.
Hi,

Thanks bro. Thx for your help. highly appreciated.

AZ.
Jul 27 '07 #5
JosAH
11,448 Expert 8TB
I hope I have not broken the laws of the scripts by giving you a fully code.
Well actually you have, but apparently it slipped through. Don't post full code
spoonfeeding solutions anymore. Read the Help link (top right) for the forum
posting guidelines. When I happen to pass by here and I see full code spoon-
feeding, I remove it; please don't do it again, i.e. it is not in the benefit of the OP
to cheat with homework assignments.

kind regards,

Jos
Jul 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: superprad | last post by:
Is there an easy way to grab the Unique elements from a list? For Example: data = what I am looking for is the unique elements 0.4 and 0.9 with their index from the list. Probably something...
2
by: jjl | last post by:
I have an array, and try to find out the unique elements (case insentitive) of array. Could someone help me out. Thanks a lot.
4
by: Victor Engmark | last post by:
When looking for a method to fetch unique elements and counting the number of occurences of each of them, I found quite a lot of gross examples of complex XSL. But after realizing the subtle...
3
by: gouqizi.lvcha | last post by:
Hi all I have a large vector with float point numbers in it, for example (1.1, 2.1 , 3.2 , 3.3 , 4, 6, 3.2, 8). Is there an easy way to determine how many uique elements in the array? Thanks...
2
by: Jo Goos | last post by:
Hello, I would like to count the unique values of a specific element in an XPath statement. Let's say I have the next XML document ... <CLUB> <MEMBER> <NAME>Fred</NAME>...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
6
by: Tekkaman | last post by:
I have a list of lists and I want to define an iterator (let's call that uniter) over all unique elements, in any order. For example, calling: sorted(uniter(, , ])) must return . I tried the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...

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.