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.
5 39973
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
Yes, the arrays are sorted. Thanks for your reply.
AZ.
Actually there are 4 unique elements in your example array, zero is also a number. Since the numbers are sorted then it is simple: -
#include <iostream>
-
using namespace std;
-
int main()
-
{
-
const int size = 7;
-
int array[size] = {0,0,0,1,2,3,3};
-
int unique = 1; //incase we have only one element; it is unique!
-
for(int i = 0; i < size -1 /*since we don't want to compare last element with junk*/; i++)
-
{
-
if(array[i]==array[i+1])
-
continue;
-
else
-
unique++;
-
}
-
cout<<"The number of unique elements is "<<unique<<endl;
-
return 0;
-
}
-
I hope I have not broken the laws of the scripts by giving you a fully code.
Actually there are 4 unique elements in your example array, zero is also a number. Since the numbers are sorted then it is simple: -
#include <iostream>
-
using namespace std;
-
int main()
-
{
-
const int size = 7;
-
int array[size] = {0,0,0,1,2,3,3};
-
int unique = 1; //incase we have only one element; it is unique!
-
for(int i = 0; i < size -1 /*since we don't want to compare last element with junk*/; i++)
-
{
-
if(array[i]==array[i+1])
-
continue;
-
else
-
unique++;
-
}
-
cout<<"The number of unique elements is "<<unique<<endl;
-
return 0;
-
}
-
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
|
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...
|
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...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
| |