-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Codility missing element (Go/Java)
- Loading branch information
1 parent
af00c44
commit 32c181e
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package algorithms | ||
|
||
func PermMissingElement(haystack []int) uint { | ||
hayset := make(map[int]struct{}, len(haystack)) | ||
|
||
for _, v := range haystack { | ||
hayset[v] = struct{}{} | ||
} | ||
|
||
// Will always return at least element because the loop goes to | ||
// one past haystack's length | ||
for i := 1; i < len(haystack) + 2; i++ { | ||
if _, found := hayset[i]; !found { | ||
return uint(i) | ||
} | ||
} | ||
|
||
panic("Unreachable") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package jobspls.algorithms; | ||
|
||
import java.util.Arrays; | ||
import java.util.TreeSet; | ||
|
||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class PermMissingElement { | ||
public static int missing_element(int[] haystack) { | ||
var hayset = new TreeSet<Integer>(Arrays.stream(haystack) | ||
.boxed() | ||
.collect(Collectors.toList())); | ||
|
||
// Should always find a value. | ||
return IntStream.range(1, haystack.length + 2) | ||
.filter(n -> { return !hayset.contains(n); }) | ||
.findFirst() | ||
.getAsInt(); | ||
} | ||
} |