Find Duplicate Value in Multi-Dimensional Array

PHP: Check for duplicate values in a multidimensional array

This will remove duplicate items from your array using array_unique():

$new_arr = array_unique($arr, SORT_REGULAR);

How to detect duplicate values in multidimensional associative array?

Simple solution is:

<?php

$data = [
[
'name' => 'name 1',
'phone' => '12341234',
'email' => 'test@web.com'
],
[
'name' => 'name 2',
'phone' => '12341234',
'email' => 'test@web1.com'
],
[
'name' => 'name 3',
'phone' => '4322342',
'email' => 'test@web1.com'
],
[
'name' => 'name 4',
'phone' => '1234123423',
'email' => 'test@web1.com'
],
[
'name' => 'name 5',
'phone' => '12341266634',
'email' => 'test@eqweqwweb.com'
],
];

$phones = [];
$emails = [];
foreach ($data as $key => $contact) {
if (array_search($contact['phone'], $phones) !== false || array_search($contact['email'], $emails) !== false) {
unset($data[$key]);
} else {
$phones[] = $contact['phone'];
$emails[] = $contact['email'];
}
}

var_dump($data);

and in result you'll get:

array(3) {
[0] =>
array(3) {
'name' =>
string(6) "name 1"
'phone' =>
string(8) "12341234"
'email' =>
string(12) "test@web.com"
}
[2] =>
array(3) {
'name' =>
string(6) "name 3"
'phone' =>
string(7) "4322342"
'email' =>
string(13) "test@web1.com"
}
[4] =>
array(3) {
'name' =>
string(6) "name 5"
'phone' =>
string(11) "12341266634"
'email' =>
string(18) "test@eqweqwweb.com"
}
}

this is just example.

How to find or count the duplicate in multidimensional array in c#

You can try HashSet<int>, e.g.

   int[,] arr = new int[3, 3] {
{1, 2, 6 },
{4, 1, 5 },
{6, 1, 8 }
};

HashSet<int> unique = new HashSet<int>();

foreach (var item in arr)
if (!unique.Add(item))
Console.WriteLine(item); // Not unique, print it out

Outcome:

  1
6
1

If we want to print each duplicate once, we can add another HashSet<int>:

   HashSet<int> unique = new HashSet<int>();
HashSet<int> duplicates = new HashSet<int>();

foreach (var item in arr)
if (!unique.Add(item))
duplicates.Add(item);

foreach (var item in duplicates)
Console.WriteLine(item);

Finally, if you want to count duplicate occurences we can change HashSet<int> duplicates into Dictionary<int, int> duplicates:

   HashSet<int> unique = new HashSet<int>();

Dictionary<int, int> duplicates = new Dictionary<int, int>();

foreach (var item in arr)
if (!unique.Add(item))
if (duplicates.TryGetValue(item, out int count))
duplicates[item] = count + 1;
else
duplicates[item] = 2;

foreach (var item in duplicates)
Console.WriteLine($"{item.Key} appears {item.Value} times");

Edit: you can change foreach loop into nested for ones, e.g:

All duplicates

   for (int i = 0; i < arr.GetLength(0); ++i)
for (int j = 0; j < arr.GetLength(1); ++j)
if (!unique.Add(arr[i, j]))
Console.WriteLine(arr[i, j]);

Distinct duplicates

   HashSet<int> duplicates = new HashSet<int>();

for (int i = 0; i < arr.GetLength(0); ++i)
for (int j = 0; j < arr.GetLength(1); ++j)
if (!unique.Add(arr[i, j]))
if (duplicates.Add(arr[i, j])) // print distinct duplicate only
Console.WriteLine(arr[i, j]);

How to remove duplicate values from a multi-dimensional array in PHP

Here is another way. No intermediate variables are saved.

We used this to de-duplicate results from a variety of overlapping queries.

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

How can I get the duplicate multidimensional array in php

// first : get all data, if the data same / duplicate take only one data

$unique = array_unique($data, SORT_REGULAR);

// then, get the data which duplicate with

$diffCellUniq = array_diff_key($data, $unique);

// so print the result

print_r($diffCellUniq); exit;

Array
(
[2] => Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
)

)

find duplicate value in multi-dimensional array

<?php
$array = array(array(7,18), array(12,7), array(12, 7, 13));
$result = array();

$first = $array[0];
for($i=1; $i<count($array); $i++){
$result = array_intersect ($first, $array[$i]);
$first = $result;
}
print_r($result);//7
?>

how do find duplicate values in a multidimensional array

Suppose you have this data:

var arrayLastFirst = [
{first: [1, 2], last: [3, 4]},
{first: [5, 6], last: [6, 7]},
{first: [0, 8], last: [8, 9]}
];

This test condition

arrayLastFirst[i].first[0] &&
arrayLastFirst[i].first[1] == arrayLastFirst[i].last[0] &&
arrayLastFirst[i].last[1]

means that you want

  • element 0 of first to be not null
  • AND element 1 of last to be not null
  • AND element 1 of first to be equal to element 0 of last.

This leads to this array:

array == [[5, 6], [6, 7]];

Multi-dimensional array return keys with duplicate values

Edit: I've updated the answer quite a bit.

Edit 2: Now utilizing the built in array functions to find the duplicates

$products = [
0 => ['product-id' => 124],
1 => ['product-id' => 125],
2 => ['product-id' => 124],
3 => ['product-id' => 126],
4 => ['product-id' => 126],
8 => ['product-id' => 124],
];

// Find the duplicates
$product_ids = array_column($products, 'product-id');
$count = array_count_values($product_ids);
$duplicates = array_filter($count, function($var) {
return $var > 1;
});

// List all the entries with duplicate ids:
foreach ( array_flip($duplicates) as $product_id ) {
$filter = array_filter($products, function($var) use ($product_id) {
return ( $var['product-id'] === $product_id );
});
print_r('Product-id: ' . $product_id . ' is duplicated in entries: ');
print_r(array_keys($filter));
}

The output:

// Product-id: 124 is duplicated in entries: Array
// (
// [0] => 0
// [1] => 2
// [2] => 8
// )
// Product-id: 126 is duplicated in entries: Array
// (
// [0] => 3
// [1] => 4
// )

Finding duplicate values in multidimensional array

$output = array();
// loop through each user
foreach ($users as $id => $ips) {
// loop through each IP address
foreach ($ips as $ip) {
// add IP address, if not present
if (!isset($output[$ip])) {
$output[$ip] = array();
}
// add user ID to the IP address' array
$output[$ip][] = $id;
}
}


Related Topics



Leave a reply



Submit