Examples

Page 6 of 50

Browse code examples

goSnippet

Kadane Max Subarray Sum

Sweep a slice once with Kadane: reset the running sum when it turns negative, track the best window, and print both the sum and the winning subarray.

goSnippet

Matrix Transpose With Slices

Rebuild a matrix with rows and columns exchanged through nested loops over a fresh [][]int, keeping the source untouched for shape-aware transformations.

goSnippet

Send On Closed Channel Panics

Receiving from a closed channel stays safe and drains buffered values before zero-and-false, while a send panics until recover catches it.

goSnippet

Sentinel Value Stops Workers

A reserved sentinel job value makes a worker exit early, and closing the jobs channel stops the remaining workers waiting on receive.

goSnippet

Single Number Found By Xor

Cancel out duplicate values with a single xor accumulator: identical pairs annihilate, leaving the one number that appears exactly once in the slice.

javaSnippet

Current Working Directory

user.dir anchors relative paths for the whole JVM: Paths.get("").toAbsolutePath() lands on the same directory that System.getProperty("user.dir") reports.

javaSnippet

Double NaN Never Equals Itself

NaN compares false against everything including itself: use Double.isNaN for detection and Double.compare for ordering, which treats NaN as equal to itself.

javaSnippet

Read Symbolic Link Target Path

Create a symlink and read back the raw target stored inside it with Files.readSymbolicLink, keeping toRealPath as the follow-the-link view of the same path.