ESP32 cpplinq: Removing duplicate elements of array

In this tutorial we will learn how to remove duplicate elements from an array of integers, using cpplinq. The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

Introduction

In this tutorial we will learn how to remove duplicate elements from an array of integers, using cpplinq.

We will be using the ESP32 as target board and we will also use the Arduino core to program the device.

The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

The code

The first thing we will do is including the cpplinq library. This way we will have access to the cpplinq operator that allows to remove duplicate elements from an array.

#include "cpplinq.hpp"

Then we will declare the use of the cpplinq namespace, like we have been doing in the previous tutorials. This way, we don’t need to use the C++ scope resolution operator, making the syntax shorter.

using namespace cpplinq;

In the Arduino setup function, we will start by opening a serial connection. That way, when testing the code, we can check the results on the Arduino IDE serial monitor.

Serial.begin(115200);

Then we will declare an array of integers with some duplicate values, so we can later remove them.

int ints[] = {1,1,1,2,2,3,4,5,6,7,7,7,7};

As we have been doing in the previous tutorials using cpplinq, the first thing we need to do before applying operators is converting the array to a range object.

from_array(ints)

Then, to remove the duplicate elements from the range, we simply need to use the distinct operator. This operator takes no arguments and will ensure only distinct values will remain on the resulting range.

distinct()

Finally, we will convert the range to a C++ vector, so we can iterate over its elements and confirm that only distinct values remained.

The full expression tree can be seen below and it already contains the to_vector operator call, which converts the range into a vector.

auto result = from_array(ints)
               >> distinct()
               >> to_vector();

After this we just need to iterate over the vector and print all the elements, so we can check the final values it contains.

for(int i=0; i<result.size(); ++i){
    Serial.print(result[i]);
    Serial.print("|");
}

The final code can be seen below.

#include "cpplinq.hpp"
 
using namespace cpplinq;
 
void setup() {
  Serial.begin(115200);
   
  int ints[] = {1,1,1,2,2,3,4,5,6,7,7,7,7};
   
  auto result = from_array(ints)
               >> distinct()
               >> to_vector();
                
  for(int i=0; i<result.size(); ++i){
    Serial.print(result[i]);
    Serial.print("|");
  }
}
 
void loop() {}

Testing the code

To test the previous code, simply compile it and upload it to your ESP32, using the Arduino IDE.

When the procedure finishes, open the IDE serial monitor. You should get an output similar to figure 1. As can be seen, all the duplicate elements were removed from the array, as expected.

Output of the program, showing the final array with duplicates removed by the cpplinq distinct operator.
Figure 1 – Output of the program, showing the array without duplicates.

Related Posts

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading