More Elegant Way to Check for Duplicates in C++ Array

More elegant way to check for duplicates in C++ array?

You could sort the array in O(nlog(n)), then simply look until the next number. That is substantially faster than your O(n^2) existing algorithm. The code is also a lot cleaner. Your code also doesn't ensure no duplicates were inserted when they were re-entered. You need to prevent duplicates from existing in the first place.

std::sort(userNumbers.begin(), userNumbers.end());
for(int i = 0; i < userNumbers.size() - 1; i++) {
if (userNumbers[i] == userNumbers[i + 1]) {
userNumbers.erase(userNumbers.begin() + i);
i--;
}
}

I also second the reccomendation to use a std::set - no duplicates there.

Algorithm: efficient way to remove duplicate integers from an array

How about:

void rmdup(int *array, int length)
{
int *current , *end = array + length - 1;

for ( current = array + 1; array < end; array++, current = array + 1 )
{
while ( current <= end )
{
if ( *current == *array )
{
*current = *end--;
}
else
{
current++;
}
}
}
}

Should be O(n^2) or less.

Check if all content of arrays are between a range of numbers, without duplicates

If i understood your question, you have to do another check into array to see the duplicate.

#include <iostream>
using namespace std;
int main() {
bool somethingBadHappend=false; //booleans like this make it easy for the program to be read
int numbers[15]{};

cout << "enter 15 numbers:";
for (int i = 0; i < 15; i++)
{
cin>>numbers[i]; //entering them into the array
}

//to find values >15 or <1
for (int i = 0; i < 15; i++)
{
if (numbers[i] > 15 || numbers[i]<=0)
{
somethingBadHappend = true;
}
}
//to find the duplicate into array
for (int i = 0; i < 15; i++){
for(int c=0; c<15; c++){
if(i!=c){ // check the different index
if(numbers[i]==numbers[c]){
somethingBadHappend = true; //found duplicate
break;
}
}
}
}

if (somethingBadHappend) //see the perfect use of code here?
{
cout<<"NOT GOOD"; // How elegant!
}
else
cout<<"GOOD";

return 0;
}

More elegant way to check for duplicates in C++ array?

You could sort the array in O(nlog(n)), then simply look until the next number. That is substantially faster than your O(n^2) existing algorithm. The code is also a lot cleaner. Your code also doesn't ensure no duplicates were inserted when they were re-entered. You need to prevent duplicates from existing in the first place.

std::sort(userNumbers.begin(), userNumbers.end());
for(int i = 0; i < userNumbers.size() - 1; i++) {
if (userNumbers[i] == userNumbers[i + 1]) {
userNumbers.erase(userNumbers.begin() + i);
i--;
}
}

I also second the reccomendation to use a std::set - no duplicates there.

Better solution to find and return duplicates in an array - VBA

Double Dictionary

As String (Exactly the Same Functionality)

Sub test1()
Dim allFruits(9) As String, manyFruits() As String
allFruits(0) = "plum"
allFruits(1) = "apple"
allFruits(2) = "orange"
allFruits(3) = "banana"
allFruits(4) = "melon"
allFruits(5) = "plum"
allFruits(6) = "kiwi"
allFruits(7) = "nectarine"
allFruits(8) = "apple"
allFruits(9) = "grapes"
manyFruits = Duplicates1(allFruits())
Debug.Print Join(manyFruits, vbLf)
End Sub

Function Duplicates1(StringArray() As String) As String()

Dim sDict As Object: Set sDict = CreateObject("Scripting.Dictionary")
sDict.CompareMode = vbTextCompare
Dim dDict As Object: Set dDict = CreateObject("Scripting.Dictionary")
dDict.CompareMode = vbTextCompare

Dim n As Long
For n = LBound(StringArray) To UBound(StringArray)
If sDict.Exists(StringArray(n)) Then
dDict(StringArray(n)) = Empty
Else
sDict(StringArray(n)) = Empty
End If
Next n
If dDict.Count = 0 Then Exit Function
Set sDict = Nothing

Dim arr() As String: ReDim arr(0 To dDict.Count - 1)
Dim Key As Variant
n = 0

For Each Key In dDict.Keys
arr(n) = Key
n = n + 1
Next Key

Duplicates1 = arr

End Function

As Variant (Shorter But Different see ' ***)

Sub test2()
Dim allFruits(9) As String, manyFruits() As Variant ' *** here
allFruits(0) = "plum"
allFruits(1) = "apple"
allFruits(2) = "orange"
allFruits(3) = "banana"
allFruits(4) = "melon"
allFruits(5) = "plum"
allFruits(6) = "kiwi"
allFruits(7) = "nectarine"
allFruits(8) = "apple"
allFruits(9) = "grapes"
manyFruits = Duplicates2(allFruits())
Debug.Print Join(manyFruits, vbLf)
End Sub

Function Duplicates2(StringArray() As String) As Variant ' *** here

Dim sDict As Object: Set sDict = CreateObject("Scripting.Dictionary")
sDict.CompareMode = vbTextCompare
Dim dDict As Object: Set dDict = CreateObject("Scripting.Dictionary")
dDict.CompareMode = vbTextCompare

Dim n As Long
For n = LBound(StringArray) To UBound(StringArray)
If sDict.Exists(StringArray(n)) Then
dDict(StringArray(n)) = Empty
Else
sDict(StringArray(n)) = Empty
End If
Next n

Duplicates2 = dDict.Keys

End Function

Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array

You could sort the array and then run through it and then see if the next (or previous) index is the same as the current. Assuming your sort algorithm is good, this should be less than O(n2):

const findDuplicates = (arr) => {

let sorted_arr = arr.slice().sort(); // You can define the comparing function here.

// JS by default uses a crappy string compare.

// (we use slice to clone the array so the

// original array won't be modified)

let results = [];

for (let i = 0; i < sorted_arr.length - 1; i++) {

if (sorted_arr[i + 1] == sorted_arr[i]) {

results.push(sorted_arr[i]);

}

}

return results;

}

let duplicatedArray = [9, 9, 111, 2, 3, 4, 4, 5, 7];

console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);


Related Topics



Leave a reply



Submit