-1

So I need to write program which calculates biggest non-repetitive element in given array. Currently I wrote program to calculate biggest one, but I can't write algorithm to sort non repetitive elements in new array.. MY CODE:

#include <iostream>
#include <iomanip>
#include <algorithm>

using namespace std;
int main()
{
int n,a[20],b[20],did=0;
cin >> n;
for(int i=0;i<n;i++)
 {
  cin >> a[i]; // reading input 
 }
for(int i=0;i<n;i++) // biggest
 {
  did=a[0];
   if(did<a[i])
   did=a[i];
 }
for(int i=0;i<n;i++) // trying to write non repetitive ones in new array
  {
   for(int j=i+1;j<n;j++)
    {
     if(a[i]!=a[j])
     b[i]=a[j];
    }
  }
for(int i=0;i<n;i++) // biggest from non-repetitive array
  {
   if(b[i]>b[i-1] && b[i]>b[i+1] && b[i]==did)
   cout << b[i];
  }

}
  • 1
    Can you show an example and the intended result? – Matthieu Brucher Oct 31 '18 at 18:15
  • I can't quite tell what you're asking here. Surely you can find the largest element? And you can check whether an element repeats? – Caleb Oct 31 '18 at 18:15
  • You probably want `did=a[0];` to be outside the loop, otherwise you're just comparing the first element to every element every time... – scohe001 Oct 31 '18 at 18:16
  • 5 // how much elements 5 3 1 4 5 // elements 4 // biggest non repetitive – Martynas Buiv Oct 31 '18 at 18:16
  • 1
    See also [here](https://www.geeksforgeeks.org/non-repeating-element/). – Nico Schertler Oct 31 '18 at 18:16
  • 1
    A couple things worth improving: use `std::vector` instead of arrays; put some spaces around binary operators and after commas/semicolons to make the code more readable; make use of [standard algorithms](https://en.cppreference.com/w/cpp/algorithm) instead of a bunch of for loops. – Pezo Oct 31 '18 at 18:17
  • yes its duplication, but other one is way too advanced and I need better explanation.. – Martynas Buiv Oct 31 '18 at 18:19
  • @MartynasBuiv I realize you're new here and I don't mean to be unkind, but SO isn't a homework-answering service. What specific problem, other than not having the answer, is stopping you from making progress? There must be 10 good solutions to this problem, and you'll learn a lot more if you give this problem your best shot. How would you solve this problem in real life if someone gave you a box of different-sized bolts and told you to find the largest unique one in the box? – Caleb Oct 31 '18 at 18:34
  • I am siting to this problem 2 hours, tried litteraly everything in my control and came here to seek help.. – Martynas Buiv Oct 31 '18 at 18:38

1 Answers1

0

(I assume that the array is not empty, & has also non-repetitive positive values)

Something like that?

#include <vector>
#include <map>

int main()
{
    std::map<int, int> m;

    // The "array":
    std::vector<int> ar{3, 3, 4, 6, 7, 800, 123, 245, 245, 700, 800, 800};

    // Count number of repetitions for each element: 
    for (auto el : ar)
        m[el]++;

    // Extract the biggest that has only one appearance:
    int result = 0;
    for (auto iter = m.rbegin(); iter != m.rend(); ++iter) {
        if (iter->second == 1) {
            result = iter->first;
            break;
        }
    }

    return result; // 700 in this example
}

.

If you insist on C array (you can also use std::array):

std::map<int, int> m;

// The array:
const size_t ARRAY_SIZE = 13;
int ar[ARRAY_SIZE] { 3, 3, 4, 5, 6, 7, 800, 123, 245, 245, 700, 800, 800 };

// Count number of repetitions for each element: 
for (size_t i = 0; i < ARRAY_SIZE; i++)
    m[ar[i]]++;

// From here is the same code as in the std::vector use example
Amit G.
  • 2,546
  • 2
  • 22
  • 30