least frequent common number from a int array - c++

I have to find least common number from an int array , I have written code but it is not working properly ,
Here is my logic,
1. sort the array
2. get min common counter updated
3. get if all are unique
and the code below,
static int min_loc ; //minimum value location
static int min_cnt ;
int all_uniqFlag = true;
void leastCommon(int data[],int n)
{
int rcount = 0; //Repeated number counter
int mcount = n; // minimum repetetion counter;
// The array is already sorted we need to only find the least common value.
for(int i = 0 ; i < n-1 ; i++)
{
//Case A : 1 1 2 2 2 3 3 3 3 4 5 5 5 5 : result should be 4
//Case B : 1 2 3 4 5 6 7 (All unique number and common values so all values should be printed
// and )
//Case C : 1 1 2 2 3 3 4 4 (all numbers have same frequency so need to display all )
cout << "data[i] : " << data[i] << " data[i+1] : " << data[i+1] << "i = " << i << endl;
if(data[i] != data[i+1])
{
//mcount = 0;
//min_loc = i;
//return;
}
if(data[i] == data[i+1])
{
all_uniqFlag = false;
rcount++;
}
else if(rcount < mcount)
{
mcount = rcount;
min_loc = i ;//data[i];
}
}
min_cnt = mcount;
}
As mentioned in the comment only Case B works and Case A and C is not working could you help me fix the issue ?

scan through the list
compare each element in the list with the last element in the out array
If the element matches, then increment its count by 1
If the element doesn't match then add the new element into out
array and increment index by 1
Once the scan is done, the out array will have all the distinct elementsout[][0] and their frequencies out[][1]
Scan through the frequency list (out[][1]) to find the lowest frequency
Finally do another scan through the element list out[][0] and print elements whose frequency matches with the lowest frequency
.
#include<stdio.h>
#include<stdlib.h>
#define N 8
int main()
{
//int data[N]={1,2,3,4,5,6,7};
int data[N]={1,1,2,2,3,3,4,4};
//int data[N]={1,1,2,2,2,3,3,3,3,4,5,5,5,5};
int out[N][2];
int i=0,index=0;
for(i=0;i<N;i++)
{
out[i][0]=0;
out[i][1]=0;
}
out[0][0] = data[0];
out[0][1]=1;
for(i=1;i<N;i++)
{
if(data[i] != out[index][0])
{
index++;
out[index][0] = data[i];
out[index][1] = 1;
}
else
{
out[index][1]++;
}
}
int min=65536;
for(i=0;i<N;i++)
{
if(out[i][1] == 0)
{
break;
}
if(out[i][1] < min)
{
min = out[i][1];
}
}
for(i=0;i<N;i++)
{
if(out[i][1] == min)
{
printf("%d\t",out[i][0]);
}
}
printf("\n");
}

You can use a map for this:
#include <string>
#include <map>
#include <iostream>
typedef std::map<int, int> Counter;
void leastCommon(int data[],int n) {
Counter counter;
int min = n;
for (int i = 0; i < n; i++)
counter[data[i]]++;
for (Counter::iterator it = counter.begin(); it != counter.end(); it++) {
if (min > it->second) min = it->second;
}
for (int i = 0; i < n; i++) {
if (counter[data[i]] == min) {
std::cout << data[i] << std::endl;
counter[data[i]]++;
}
}
}
int main() {
int data[] = {1, 1,3,4,4,2,4,3,2};
leastCommon(data, 9);
return 0;
}

Approach is-
select 1st element from the sorted array, and while consecutive elements to it are same, store them in output[] until the loop breaks
store the frequency of element in leastFrequency
select next element, check with its consecutive ones and store them in same output[] until the loop breaks
check frequency of this with the leastFrequency
if same, do nothing (let these be added in the output[])
if less, clear output[] and store the element same no. of times
if more, change the effective output[] length to previous length before iterating for this element
similarly iterate for all distinct elements and finally get the result from output[] from 0 to effective length
void leastCommon(int data[], int len) {
if ( len > 0) {
int output[] = new int[len];
int outlen = 0; // stores the size of useful-output array
int leastFrequency = len; // stores the lowest frequency of elements
int i=0;
int now = data[i];
while (i < len) {
int num = now;
int count = 0;
do {
output[outlen] = now;
outlen++;
count++;
if((++i == len)){
break;
}
now = data[i];
} while (num == now); // while now and next are same it adds them to output[]
if (i - count == 0) { // avoids copy of same values to output[] for 1st iteration
leastFrequency = count;
} else if (count < leastFrequency) { // if count for the element is less than the current minimum then re-creates the output[]
leastFrequency = count;
output = new int[len];
outlen = 0;
for (; outlen < leastFrequency; outlen++) {
output[outlen] = num; // populates the output[] with lower frequent element, to its count
}
} else if (count > leastFrequency) {
outlen -= count; // marks outlen to its same frequent numbers, i.e., discarding higher frequency values from output[]
}
}
//for(int j = 0; j < outlen; j++) {
// print output[] to console
//}
}
}
Plz suggest for improvements.

Related

Stuck on recursion algorithm in arrays

I got stuck with the following task:
"Write a recursive function that takes a one-dimensional array of 100 randomly set integers and finds the position at which a sequence of 10 numbers begins with the minimum sum".
I've written 2 functions:
int recursiveArrSum(int mass[], int dim = 10, int sum = 0) {
if (dim == 0) return sum;
sum += mass[dim-1];
return recursiveArrSum(mass, --dim, sum);
}
int recArrMinSum(int mass[], int dim=100, int tempSum=100, int idx=0, int addPar=0){
if (dim == 0) return idx;
mass[dim]=mass[addPar];
if (tempSum >= recursiveArrSum(mass)){
tempSum = recursiveArrSum(mass);
idx = dim-1;
//mass[dim]=mass[addPar];
}return recArrMinSum(mass, --dim, tempSum, idx, ++addPar);
1st one (recursiveArrSum) - works fine, but the second one drives me crazy...
I can't understand how I need to iterate an array during evoking the function on last return statement. I made it using for loop, and it works correctly:
for (int i=0; i<91; i++){
int tempS=0;
for (int j=i; j<=i+9; j++){
tempS += arr[j];
cout << tempS<< endl;
}
if (tempS<tempSum) {
tempSum=tempS;
k=i+1;
}
}
but recursion doesn't...
Could anyone suggest me the way for solving this issue?
Open for any questions.
Thanks in advance.
This is how I would do it:
A function that keeps in its arguments the current position, the current sum, the minimum sum and the starting position of this minimum sum. The function also transports as arguments the array (obviously), its size and the dimension of the sum (10 in your case).
void compute(int mass[], int massSize = 100, int dim = 10, int currentPosition = 0, int currentSum = 0, int minSum = 99999, int minPosition = -1)
{
// Before suming the first dim elements, we keep going adding the element at the current position and we increase the current position.
if (currentPosition < dim)
{
compute(mass, massSize, dim, currentPosition + 1, currentSum + mass[currentPosition], minSum, minPosition);
return;
}
// When reaching the end of the array, we print our best solution.
if (currentPosition > massSize)
{
std::cout << "Min sum of " << dim << " elements is " << minSum << ", starting at position " << minPosition << std::endl;
return;
}
// In all the other cases, we check if the current sum is better than the minimum sum. If yes, we update the minimum sum and its starting position. Then call again the function!
if (currentSum < minSum)
{
minSum = currentSum;
minPosition = currentPosition - dim;
}
compute(mass, massSize, dim, currentPosition + 1, currentSum + mass[currentPosition ] - mass[currentPosition -dim], minSum, minPosition);
}
Calling the function is easy: compute(mass, SIZE, 10); where mass is your array, SIZE is its size and 10 is your dimension.
With a dimension equals to 5 in this array:
8; 8; 4; 10; 9; 9; 6; 3; 3; 5; 3; 8; 3; 7; 9; 10; 10; 5; 6; 4; 1; 4; 1; 5; 2; 8; 6; 1; 7; 9;
Min sum of 5 elements is 13, starting at position 20
When writing a recursive function, the first things to do is to think about the exit case, the init cases (if there are any) and the "classic" case when you're in the middle of your search.
Feel free to ask any question, I added some comments to make it clear enough.
It is unclear how your code is supposed to work. To iterate the array and find the smallest sum you do something like this (pseudo code):
int currentSum = sum of elements 0 till 9
int smallestSum = currentSum
int smallestIndex = 0
for (i = 10; i < 100; ++i) {
currentSum = currentSum - mass[ i -10] + mass[i]
if (currentSum < smallestSum) {
smallestSum = currentSum
smallestIndex = i
}
}
As initial guess it takes the sum of the first 10 elements, ie elements 0 till 9. After that it iterates the array. Sum of elements 1 till 10 is the same as sum of elements 0 till 9 minus first element plus element 10. More general: To get the sum in the next iteration, element at i-10 is subtracted and element at i is added.
Not exactly sure what you're allowed to include from the STL, but there's this solution:
// Or std::pair<int, int*>
struct resultPair {
int first;
int* second;
};
// Or std::accumulate
int sumOf(int* arr, size_t s) {
int sum = 0;
for (size_t i=0; i<s; ++i)
sum += arr[i];
return sum;
}
resultPair minSeqOfN(int* arr, size_t s, size_t N) {
if (s < N) throw; // Or however you want to check pre-conditions
int sum = sumOf(arr, N);
if (s == N) return { sum, arr }; // Base case
auto nextRes = minSeqOfN(arr+1, s-1, N);
if (nextRes.first < sum) return nextRes;
else return resultPair{sum, arr};
}
The function keeps calling itself minus the first element, until the sequence size is equal to the input array size (then there's just 1 possible answer).

c++ program return "0 0" instead of returning prime numbers. Why does it not get the correct output from the "prime" function?

My function "prime" takes a dynamic array and returns a dynamically allotted array consisting of only prime numbers. But when it is returned to main, it gives the wrong output and print
Output:
0 0
Expected Output:
2 3
My Code:
#include<stdio.h>
#include<math.h>
#include <cstdio>
#include<iostream>
using namespace std;
const int capacity = 10000;
int * prime(int numbers[capacity])
{
int *primeArray;
primeArray = new int[capacity];
int lenghtOfArray=sizeof(primeArray)/sizeof(int);
int counter = 0;
for(int index=0;index<lenghtOfArray;index++){
// 0 and 1 are not prime numbers
if (numbers[index] == 0 || numbers[index] == 1) {
continue;
}
else {
for (int i = 2; i <= numbers[index] / 2; ++i) {
if (numbers[index] % i == 0) {
primeArray[counter] = numbers[index];
counter = counter + 1;
break;
}
}
}
}
return primeArray;
}
main()
{
int *mynumbers;
mynumbers = new int[capacity];
mynumbers[0] = 1;
mynumbers[1] = 2;
mynumbers[2] = 3;
mynumbers[3] = 4;
mynumbers[4] = 5;
mynumbers[5] = 6;
int* myprime = prime(mynumbers);
cout << myprime[0] << " " << myprime[1];
}
What went wrong?
int lenghtOfArray=sizeof(primeArray)/sizeof(int);
is an wrong way to get the length of dynamically allocated array.
sizeof(primeArray) is not the allocated size but the size of pointer.
You already have the number of elements of the array, so use that to get the length of the array:
int lenghtOfArray=capacity;
But this doesn't make your code correct because now your code will use indeterminate values of uninitialized elements of int array. What you have to do is to pass the number of valid elements to the function.
Points to change:
// add argument to pass the number of data
// int * prime(int numbers[capacity])
int * prime(int numbers[capacity], int num_numbers)
{
int *primeArray;
primeArray = new int[capacity];
// use the passed number
// int lenghtOfArray=sizeof(primeArray)/sizeof(int);
int lenghtOfArray=num_numbers;
// pass the number of data
// int* myprime = prime(mynumbers);
int* myprime = prime(mynumbers, 6);
cout << myprime[0] << " " << myprime[1];
}
Another point is that your code is storing numbers that are not primes instead of prime numbers.
Instead of this
for (int i = 2; i <= numbers[index] / 2; ++i) {
if (numbers[index] % i == 0) {
primeArray[counter] = numbers[index];
counter = counter + 1;
break;
}
}
you should use this
// add the number for now
primeArray[counter] = numbers[index];
counter = counter + 1;
for (int i = 2; i <= numbers[index] / 2; ++i) {
if (numbers[index] % i == 0) {
// it is not actually a prime, rollback
counter = counter - 1;
break;
}
}

Generate numbers from 2 to 10,000. The numbers printed can only be a multiple of 2 prime numbers

Homework: I'm just stumped as hell. I have algorithms set up, but I have no idea how to code this
Just to be clear you do not need arrays or to pass variables by reference.
The purpose of the project is to take a problem apart and using Top-Down_Design or scratch pad method develop the algorithm.
Problem:
Examine the numbers from 2 to 10000. Output the number if it is a Dual_Prime.
I will call a DualPrime a number that is the product of two primes. Ad where the two primes are not equal . So 9 is not a dual prime. 15 is ( 3 * 5 ) .
The output has 10 numbers on each line.
My Algorithm set-up
Step 1: find prime numbers.:
bool Prime_Number(int number)
{
for (int i = 2; i <= sqrt(number); i++)
{
if (number % 1 == 0)
return false;
}
return true;
}
Step 2: store prime numbers in a array
Step 3: Multiply each array to each other
void Multiply_Prime_Numbers(int Array[], int Size)
{
for (int j = 0; j < Size- 1; j++)
{
Dual_Prime[] = Arr[j] * Arr[j + 1]
}
}
Step 4: Bubble sort
void Bubble_Sort(int Array[], int Size) // Sends largest number to the right
{
for (int i = Size - 1; i > 0; i--)
for (int j = 0; j < i; j++)
if (Array[j] > Array[j + 1])
{
int Temp = Array[j + 1];
Array[j + 1] = Array[j];
Array[j] = Temp;
}
}
Step 5: Display New Array by rows of 10
void Print_Array(int Array[], int Size)
{
for (int i = 0; i < Size; i++)
{
cout << Dual_Prime[i] << (((j % 10) == 9) ? '\n' : '\t');
}
cout << endl;
}
I haven't learned dynamic arrays yet,
Although dynamic arrays and the sieve of Eratosthenes are more preferable, I tried to write minimally fixed version of your code.
First, we define following global variables which are used in your original implementation of Multiply_Prime_Numbers.
(Please check this post.)
constexpr int DP_Size_Max = 10000;
int DP_Size = 0;
int Dual_Prime[DP_Size_Max];
Next we fix Prime_Number as follows.
The condition number%1==0 in the original code is not appropriate:
bool Prime_Number(int number)
{
if(number<=1){
return false;
}
for (int i = 2; i*i <= number; i++)
{
if (number % i == 0)
return false;
}
return true;
}
In addition, Multiply_Prime_Numbers should be implemented by double for-loops as follows:
void Multiply_Prime_Numbers(int Array[], int Size)
{
for (int i = 0; i < Size; ++i)
{
for (int j = i+1; j < Size; ++j)
{
Dual_Prime[DP_Size] = Array[i]*Array[j];
if(Dual_Prime[DP_Size] >= DP_Size_Max){
return;
}
++DP_Size;
}
}
}
Then these functions work as follows.
Here's a DEMO of this minimally fixed version.
int main()
{
int prime_numbers[DP_Size_Max];
int size = 0;
for(int j=2; j<DP_Size_Max; ++j)
{
if(Prime_Number(j)){
prime_numbers[size]=j;
++size;
}
}
Multiply_Prime_Numbers(prime_numbers, size);
Bubble_Sort(Dual_Prime, DP_Size);
for(int i=0; i<DP_Size;++i){
std::cout << Dual_Prime[i] << (((i % 10) == 9) ? '\n' : '\t');;
}
std::cout << std::endl;
return 0;
}
The Sieve of Eratosthenes is a known algorithm which speeds up the search of all the primes up to a certain number.
The OP can use it to implement the first steps of their implementation, but they can also adapt it to avoid the sorting step.
Given the list of all primes (up to half the maximum number to examine):
Create an array of bool as big as the range of numbers to be examined.
Multiply each distinct couple of primes, using two nested loops.
If the product is less than 10000 (the maximum) set the corrisponding element of the array to true. Otherwise break out the inner loop.
Once finished, traverse the array and if the value is true, print the corresponding index.
Here there's a proof of concept (implemented without the OP's assignment restrictions).
// Ex10_TwoPrimes.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void Homework_Header(string Title);
void Do_Exercise();
void Sieve_Of_Eratosthenes(int n);
void Generate_Semi_Prime();
bool Semi_Prime(int candidate);
bool prime[5000 + 1];
int main()
{
Do_Exercise();
cin.get();
return 0;
}
void Do_Exercise()
{
int n = 5000;
Sieve_Of_Eratosthenes(n);
cout << endl;
Generate_Semi_Prime();
}
void Sieve_Of_Eratosthenes(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
memset(prime, true, sizeof(prime));
for (int p = 2; p*p <= n; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
bool Semi_Prime(int candidate)
{
for (int index = 2; index <= candidate / 2; index++)
{
if (prime[index])
{
if (candidate % index == 0)
{
int q = candidate / index;
if (prime[q] && q != index)
return true;
}
}
}
return false;
}
void Generate_Semi_Prime()
{
for (int i = 2; i <= 10000; i++)
if (Semi_Prime(i)) cout << i << "\t";
}

C++ Calculating the Mode of a Sorted Array

I have to write a C++ code that finds the median and mode of an array. I'm told that it's much easier to find the mode of an array AFTER the numbers have been sorted. I sorted the function but still cannot find the mode.
int counter = 0;
for (int pass = 0; pass < size - 1; pass++)
for (int count = pass + 1; count < size; count++) {
if (array [count] == array [pass])
counter++;
cout << "The mode is: " << counter << endl;
If the array has been sorted already, you can count the occurrences of a number at once. Then just save the number that has biggest occurrences. And you can find out the mode in only one for-loop.
Otherwise, you'll have to do more than one for-loops.
See a details example at the link below
Find-the-Mode-of-a-Set-of-Numbers
Here is the code,
int number = array[0];
int mode = number;
int count = 1;
int countMode = 1;
for (int i=1; i<size; i++)
{
if (array[i] == number)
{ // count occurrences of the current number
++count;
}
else
{ // now this is a different number
if (count > countMode)
{
countMode = count; // mode is the biggest ocurrences
mode = number;
}
count = 1; // reset count for the new number
number = array[i];
}
}
cout << "mode : " << mode << endl;
One way is that you can use Run Length encoding. In Run Length encoding, representation would be like; (Item, Its frequency).
While doing so, keep track of the maximum frequency and Item. This will give you the mode once you complete the Run Length.
for example:
1 1 2 2 2 3 3 4 5
It run length encoding would be
{1, 2}, {2, 3}, {3, 2}, {4, 1}, {5, 1}
It needs O(n) space.
This is how I did it, my solution will take a sorted vector as input. It has O(n) time complexity and can work with the case where there are more than 1 "mode" number in the vector.
void findMode(vector<double> data) {
double biggestMode = 1;
vector<double> mode, numbers;
numbers.push_back(data.at(0));
mode.push_back(1);
int count = 0;
for (int i = 1; i < data.size(); i++) {
if (data.at(i) == numbers.at(count)) {
mode.at(count)++;
}
else {
if (biggestMode < mode.at(count)) {
biggestMode = mode.at(count);
}
count++;
mode.push_back(1);
numbers.push_back(data.at(i));
}
}
for (int i = 0; i < mode.size(); i++) {
if (mode.at(i) == biggestMode)
cout << numbers.at(i) << " ";
}
cout << endl;
}
Here is the code snippet:
int number = array[0];
int mode = number;
int count = 1;
int countMode = 1;
for (int i=1; i<size; i++)
{
if (array[i] == number)
{
count++;
}
else
{
if (count > countMode)
{
countMode = count;
mode = number;
}
count = 1;
number = array[i];
}
}
cout << "mode : " << mode << endl;
There is an old adage that states "If you put 10 programmers in a room and give them the same program to code you will get 12 different results", hence my version of answering your question. It may not be as fast (I'm planning on testing it's speed versus some of the other suggestions) but I feel it is easy to understand.
#include <iostream>
using namespace std;
int main ()
{
short z[10];
short maxCount = 0, curCount = 0, cur = 0, most = 0;
for (int i = 0; i < 10; i++)
{
cout << "Enter a number: " << endl;
cin >> z[i];
}
for (int i = 0; i < 10; i++)
{
cur = z[i];
for (int a = i; a < 10; a++)
{
if (cur == z[a])
{
curCount++;
cur = z[a];
}
if (curCount > maxCount)
{
maxCount = curCount;
most = z[a];
}
}
curCount = 0;
}
cout << "the mode is : " << maxCount << ", the number is: " << most << endl;
}
int number = array[0];
int mode = number;
int count = 1;
int countMode = 1;
for (int i=1; i<size; i++)
{
if (array[i] == number)
{ // count occurrences of the current number
++count;
}
else
{ // now this is a different number
count = 1; // reset count for the new number
number = array[i];
}
if (count > countMode) {
countMode = count;
mode = number;
}
}
cout << "mode : " << mode << endl;
While Diedrei's answer is close, several people have pointed out some shortcomings such as if the mode is defined by the last numbers of the sorted array (1,2,3,3,4,4,4 would return 3 as the mode). Also, depending on the requirements on how to handle multiple modes, there will be different solutions.
This solution does several things:
Solves the issue of the mode being at the end of the array
If there are multiple modes (more than 1 number has the same number of occurrences with a count > 1), returns the smallest number as the mode
Returns -1 if there is no mode (each number only occurs once)
int number = array[0];
int mode = number;
int count = 1;
int countMode = 1;
for (int i=1; i<size; i++)
{
if (array[i] == number)
{ // increment the count of occurrences for the current number
++count;
if (count > countMode)
{
countMode = count; // this number now has the most occurrences
mode = number; // this number is now the mode
}
}
else
{ // now this is a different number
count = 1; // reset count for the new number
number = array[i]; // set the new number
}
}
if (countMode == 1) {
mode = -1; // set the mode to -1 if each number in the array occur only once
}
cout << "mode : " << mode << endl;
The "mode" is the value that occurs most often. If no number is repeated, then there is no mode for the list.
So there would be no benefit to sorting if you needed to know the "mode".
Are you sure you are not referring to the median? The median is the middle number in a set.
If you have 1,2,3,4,5 the Median (middle number) is the (total_number)/2) rounded up if it is odd, 2.5 -> 3 and our median would be 3. you can only really calculate the median if your numbers are sorted.
If you have an even number in a set 1,2,3,4,5,6
your mode is slots 3,4 (coincidentally also, 3,4)
(total_number)/2 slot and (total_number)/2 + 1 slot, for any even array of numbers.
http://www.purplemath.com/modules/meanmode.htm
This code should give you the mode. If there are equal number of two different numbers, it will output the first of such.
int count = 1, mode = 0, m = 0, i = 1;
size_t sz = sizeof(array)/sizeof(*array);
while(i != sz+1) {
if(array[i-1] != array[i]) {
if(count > m) {
mode = array[i-1];
m = count;
count = 1;
}
}
else
++count;
++i;
}
std::cout << "mode: " << mode << std::endl;
This code finds the mode in C++:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int i,j,k=0,n,repeat_max=0,cn=0;
int array1[50],mode[50],count[50]={0},c[50];
cout<<"\n inter count:\t";
cin>>n;
cout<<"\n";
for(i=0;i<n;i++)
cin>>array1[i];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(array1[i]==array1[j])
{
count[i]++;
if(count[i]>=repeat_max)
{
repeat_max=count[i];
mode[k++]=array1[i];
}
}
}
}
cout<<"\n================\n";
for(i=1;i<k;i++)
cout<<"\t mode[i]="<<mode[i]<<"\n";
cout<<"\t\n\nrepeat array:"<<repeat_max;
return 0;
}
I did it this way:
int main()
{
int mode,modecount2,modecount1;
bool is_nomode=false;
vector<int> numbers = { 15,43,25,25,25,25,16,14,93,93,58,14,55,55,55,64,14,43,14,25,15,56,78,13,15,29,14,14,16 };
sort(numbers);
//If you uncomment the following part, you can see the sorted list of above numbers
//for (int i = 0; i < numbers.size(); ++i) std::cout << numbers[i] << '\n';
//keep_window_open();
mode = numbers[0];
modecount1 = 0;
modecount2 = 1; //Obviously any number exists at least once!
for (int i = 1; i < numbers.size(); ++i) {
if(numbers[i]==numbers[i-1]) ++modecount2;
else {
if (modecount2 > modecount1) {
mode = numbers[i - 1];
modecount1 = modecount2;
}
else if (i != 1 && modecount2 == modecount1) { std::cout << "No mode!\n"; is_nomode = true; break; }
modecount2 = 1;
}
}
if(!is_nomode) std::cout << "Mode of these numbers is: " << mode << std::endl;
keep_window_open();
Also you can add another 25 to the list of numbers and see what happens if two numbers have the same occurrence!
I hope it helps.
This code uses "map" to find out the MODE from the given array.
It assumes the array is already sorted.
int findMode(int * arr, int arraySize)
{
map<int, int> modeMap;
for (int i = 0; i < arraySize; ++i) {
++modeMap[arr[i]];
}
auto x = std::max_element(modeMap.begin(), modeMap.end(),
[](const pair<int, int>& a, const pair<int, int>& b) {
return a.second < b.second; });
return x->first;
}
This is the code I've written for sorted vector
void print_mode(vector<int>& input)
{
int mode=0, count = 0;
int current_number = input[0];
int mode_number = current_number;
for (int i=0; i < input.size(); i++)
{
if (current_number == input[i])//check if the number is the same
{
count++;
}
else //this fuction works when the value are no longer the same and
//this is when it updates the mode value
{
if (count > mode)//update mode value
{
mode = count;
mode_number = current_number;
}
count = 1;// it is not reset back to zero because when it the program detect a
//different number it doesn't count it so this is to solve that issue
}
if (i == input.size() - 1)// this function before it doesn't work when the largest value
//is mode so I added this if state to solve it
{
if (count > mode)
{
mode = count;
mode_number = current_number;
}
}
current_number = input[i];//prepare for next value
}
cout << mode_number << " is the mode number and it is repeated " << mode << " times" << endl;
}
1. Finding the mode without sorting
I'm told that it's much easier to find the mode of an array AFTER the numbers have been sorted
I'm not so sure.
std::vector<std::pair<int, unsigned>> mode(const std::vector<int> &v)
{
if (v.empty())
return {};
std::unordered_set<int> seen;
unsigned max_count(0);
std::vector<std::pair<int, unsigned>> ret;
for (auto i(v.begin()); i != v.end(); ++i)
if (seen.find(*i) == seen.end())
{
const auto count(std::count(i, v.end(), *i));
if (count > max_count)
{
max_count = count;
ret = {{*i, max_count}};
}
else if (count == max_count)
ret.emplace_back(*i, max_count);
seen.insert(*i);
}
return ret;
}
The algorithm
uses a hash table (seen) to skip already seen numbers;
doesn't need a copy of the input vector;
only requires a container with forward iterator support.
Also note that for small input vectors the function can be simplified removing the hash table.
You can play with the code here.
2. Finding the mode sorting
std::vector<std::pair<int, unsigned>> mode(std::vector<int> v)
{
if (v.empty())
return {};
std::sort(v.begin(), v.end());
auto current(*v.begin());
unsigned count(1), max_count(1);
std::vector<std::pair<int, unsigned>> ret({{current, 1}});
for (auto i(std::next(v.begin())); i != v.end(); ++i)
{
if (*i == current)
++count;
else
{
count = 1;
current = *i;
}
if (count > max_count)
{
max_count = count;
ret = {{current, max_count}};
}
else if (count == max_count)
ret.emplace_back(current, max_count);
}
return ret;
}
We assume an unsorted input vector, so the function works on a copy of the original vector that is sorted and processed.
If the original vector is already sorted, the input argument can be passed by reference and the std::sort call can be removed.
You can play with the code here.
Performance
Performance depends on multiple factor (size of the input vector, distribution of values...).
E.g. if the range of the input integers is small algorithm 1 is faster than algorithm 2.
You can experiment here.
I know the question is old, but here is a clean and short code that calculates statistical mode:
std::sort(vector.begin(), vector.end());
int mode = vector[0], count = 0, countMode = 1;
int last = mode;
for (int i = 1; i < vector.size(); ++i)
{
if (vector[i] == mode) ++countMode;
else
{
if (last != vector[i]) count = 0;
++count;
}
if (count > countMode)
{
mode = vector[i];
countMode = count;
count = 0;
}
last = vector[i];
}
int findModa(int *arr, int n) {
int count=1;
int countmax=0;
int current = arr[0];
int moda = 0;
for (int i=1; i<n; i++) {
if(arr[i] == curr) {
count++;
}
else if (count>countmax) {
countmax=count;
count=1;
moda=arr[i-1];
current=arr[i];
}
current=arr[i];
}
return moda;
}

C/C++ How can I get unique value from 2 arrays?

I need to get the unique value from 2 int arrays
Duplicate is allowed
There is just one unique value
like :
int arr1[3]={1,2,3};
int arr2[3]={2,2,3};
and the value i want to get is :
int unique[]={1}
how can i do this?
im already confused in my 'for' and 'if'
this was not homework
i know how to merge 2 arrays and del duplicate values
but i alse need to know which array have the unique value
plz help me :)
and here is some code i did
int arr1[3]={1,2,3}
int arr2[3]={2,2,3}
int arrunique[1];
bool unique = true;
for (int i=0;i!=3;i++)
{
for (int j=0;j!=3;j++)
{
if(arr1[i]==arr2[j])
{
unique=false;
continue;
}
else
{
unique=true;
}
if(unique)
{
arrunique[0]=arr1[i]
break;
}
}
cout << arrunique[0];
Assuming:
You have two arrays of different length,
The arrays are sorted
The arrays can have duplicate values in them
You want to get the list of values that only appear in one of the arrays
including their duplicates if present
You can do (untested):
// Assuming arr1[], arr2[], and lengths as arr1_length
int i = 0,j = 0, k = 0;
int unique[arr1_length + arr2_length];
while(i < arr1_length && j < arr2_length) {
if(arr1[i] == arr2[j]) {
// skip all occurrences of this number in both lists
int temp = arr1[i];
while(i < arr1_length && arr1[i] == temp) i++;
while(j < arr2_length && arr2[j] == temp) j++;
} else if(arr1[i] > arr2[j]) {
// the lower number only occurs in arr2
unique[k++] = arr2[j++];
} else if(arr2[j] > arr1[i]) {
// the lower number only occurs in arr1
unique[k++] = arr1[i++];
}
}
while(i < arr1_length) {
// if there are numbers still to read in arr1, they're all unique
unique[k++] = arr1[i++];
}
while(j < arr2_length) {
// if there are numbers still to read in arr2, they're all unique
unique[k++] = arr2[j++];
}
Some alternatives:
If you don't want the duplicates in the unique array, then you can skip all occurrences of this number in the relevant list when you assign to the unique array.
If you want to record the position instead of the values, then maintain two arrays of "unique positions" (one for each input array) and assign the value of i or j to the corresponding array as appropriate.
If there's only one unique value, change the assignments into the unique array to return.
Depending on your needs, you might also want to look at set_symmetric_difference() function of the standard library. However, its treatment of duplicate values makes its use a bit tricky, to say the least.
#include <stdio.h>
#include <stdlib.h>
int cmp ( const void *a , const void *b )
{
return *(int *)a - *(int *)b;
}
int main()
{
int arr1[5] = {5,4,6,3,1};
int arr2[3] = {5, 8, 9};
int unique[8];
qsort(arr1,5,sizeof(arr1[0]),cmp);
printf("\n");
qsort(arr2,3,sizeof(arr2[0]),cmp);
//printf("%d", arr1[0]);
int i = 0;
int k = 0;
int j = -1;
while (i < 5 && k < 3)
{
if(arr1[i] < arr2[k])
{
unique[++j] = arr1[i];
i++;
}
else if (arr1[i] > arr2[k])
{
unique[++j] = arr2[k];
k++;
}
else
{
i++;
k++;
}
}
//int len = j;
int t = 0;
if(i == 5)
{
for(t = k; t < 3; t++)
unique[++j] = arr2[t];
}
else
for(t = i; t < 5; t++)
unique[++j] = arr2[t];
for(i = 0; i <= j; i++)
printf("%d ", unique[i]);
return 0;
}
This is my codes,though there is a good answer .
I didn't realize the idea that know which array have the unique value.
I also think that the right answer that you chose didn't , either.
Here is my version of the algorithm for finding identical elements in sorted arrays on Python in C ++, it works in a similar way
def unique_array(array0 : (int), array1 : (int)) -> (int):
index0, index1, buffer = 0, 0, []
while index0 != len(array0) and index1 != len(array1):
if array0[index0] < array1[index1]:
buffer.append(array0[index0])
index0 += 1
elif array0[index0] > array1[index1]:
buffer.append(array1[index1])
index1 += 1
else:
index0 += 1; index1 += 1
buffer.extend(array0[index0 : len(array0)])
buffer.extend(array1[index1 : len(array1)])
return buffer