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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# IntelliJ project files
.idea/*
.idea/
.idea
*.iml
out
gen
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ _Welcome hackers it is really **awesome** have you here! Before start contributi
| **All-About-Food** | https://github.com/clubgamma/All-About-Food |

---
[Contribution from Sean W](./contributions/Sean_W.md)

**[⬆ back to top](#contents)**
9 changes: 9 additions & 0 deletions contributions/Sean_W.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Hey Fellow Contributors and creators of this repository!

I'd like to mention that I:
* I have read the Code Of Conduct.
* I have followed all the steps of submission properly.

The contributions that I did in this project are as follows:
1. I added a php file in frontend/src/ that contains a function that retrieves the k largest elements from an array.
2. Added .gitignore, so it wouldn't add PHPStorm's files to the repo.
45 changes: 45 additions & 0 deletions frontend/src/kLargest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/*
Description: HacktoberFest2k23
Author: Sean W.
Author URI: https://www.cybersholt.com
Version: 0.0.1
*/

/**
* Retrieves the k largest elements from an array.
*
* This function sorts the entire array in descending order and then
* returns the first k elements.
*
* @param array $arr Array to get elements from.
* @param int $k Number of elements to return.
*
* @return array Returns the k largest elements from $arr.
*/
function getKLargestElements( $arr, $k ) {
// Sort the array in descending order
rsort( $arr );

// Return the first k elements
return array_slice( $arr, 0, $k );
}

// Example usage
$arr = [ 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 ];
$k = 5;

$largestElements = getKLargestElements( $arr, $k );
print_r( $largestElements );

/*
* Example output:
* Array
* (
* [0] => 9
* [1] => 6
* [2] => 5
* [3] => 5
* [4] => 5
* )
*/