Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ public function unique()
return new static(array_unique($this->items, SORT_REGULAR));
}

/**
* Returns a collection with duplicate values
* @return Collection
*/
public function duplicates()
{
$unique = array_unique($this->items);
$duplicates = array_diff_assoc($this->items, $unique);

return new static($duplicates);
}

/**
* Return a new collection with the items reverese
* @return Collection
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ public function test_unique_removes_duplicate_values()
$this->assertEquals([1, 2, 3], $unique->values()->all());
}

public function test_duplicate_values_in_collection()
{
$this->assertEquals(['Bram'], (new Collection(['Bram', 'Bram', 'John', 'Doe']))->duplicates()->values()->all());
$this->assertEquals([1, 45], (new Collection([1, 1, 2, 5, 11, 28, 45, 45, 60]))->duplicates()->values()->all());
$this->assertEquals([], (new Collection(['Bram', 'John', 'Doe', 'Jane']))->duplicates()->values()->all());
}

public function test_reverse_items_in_collection()
{
$collection = new Collection([1, 2, 3]);
Expand Down