Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Task 5 — Common Adjacent Pairs

Objective

Create a function that compares three integer slices and finds adjacent value pairs that appear in more than one slice.

For every matching pair, report:

  • the two values in the pair
  • which input slices contain that pair

Each distinct pair must appear only once in the final result.

Input

List 1

list1 := []int{
    41, 19, 25, 74, 85, 36,
    93, 47, 56, 76, 20, 39,
}

List 2

list2 := []int{
    39, 43, 56, 66, 73, 46,
    58, 93, 74, 29, 85, 36,
}

List 3

list3 := []int{
    93, 47, 74, 29, 85, 36,
    80, 25, 56, 66, 20, 39,
}

Function

Create a function named:

TakeCommonPairs(...)

The function receives three:

[]int

values and returns:

[]string

containing one description for every adjacent pair that appears in at least two input slices.

Adjacent Pair

A pair consists of two neighboring elements.

For a slice:

[]int{
    41, 19, 25, 74,
}

the adjacent pairs are:

[41,19]
[19,25]
[25,74]

A pair preserves its order.

Therefore:

[41,19]

is different from:

[19,41]

unless an implementation explicitly changes the task semantics.

Pair Generation

For a slice with length n, generate pairs using:

{list[0], list[1]}
{list[1], list[2]}
{list[2], list[3]}
...
{list[n-2], list[n-1]}

Common Pairs

A pair should be reported only when it exists in at least two different input slices.

For the provided data, the common adjacent pairs are:

[85,36]
[93,47]
[20,39]
[56,66]
[74,29]
[29,85]

Pair [85,36]

This pair exists in:

list1
list2
list3

Result description:

Pair of values [85,36] exists in lists [1,2,3].

Pair [93,47]

This pair exists in:

list1
list3

Result description:

Pair of values [93,47] exists in lists [1,3].

Pair [20,39]

This pair exists in:

list1
list3

Result description:

Pair of values [20,39] exists in lists [1,3].

Pair [56,66]

This pair exists in:

list2
list3

Result description:

Pair of values [56,66] exists in lists [2,3].

Pair [74,29]

This pair exists in:

list2
list3

Result description:

Pair of values [74,29] exists in lists [2,3].

Pair [29,85]

This pair exists in:

list2
list3

Result description:

Pair of values [29,85] exists in lists [2,3].

Expected Result

The function should return descriptions equivalent to:

Pair of values [85,36] exists in lists [1,2,3].

Pair of values [93,47] exists in lists [1,3].

Pair of values [20,39] exists in lists [1,3].

Pair of values [56,66] exists in lists [2,3].

Pair of values [74,29] exists in lists [2,3].

Pair of values [29,85] exists in lists [2,3].

Deduplication

A common pair must appear only once in the output.

For example:

[85,36]

exists in all three lists.

It must therefore produce one result:

Pair of values [85,36] exists in lists [1,2,3].

and not separate duplicate records such as:

[85,36] exists in [1,2]
[85,36] exists in [1,3]
[85,36] exists in [2,3]

Pair Identity

Pair order is significant.

For example:

[85,36]

does not match:

[36,85]

because adjacency includes the direction in which the two values appear in the slice.

Multiple Occurrences in One List

If the same adjacent pair appears multiple times inside one input slice, that slice should still be listed only once for that pair.

For example, if a pair appears twice in list1 and once in list2, its report should still contain:

lists [1,2]

rather than repeating list identifiers.

Requirements

The function must:

  1. generate all adjacent pairs from each input slice
  2. identify equivalent ordered pairs
  3. track which input slices contain every pair
  4. keep only pairs appearing in at least two different slices
  5. produce one result per distinct pair
  6. avoid duplicate reports

Result Ordering

The original task provides a specific example order but does not define a general ordering rule.

A deterministic implementation should preserve a documented ordering strategy.

One reasonable approach is to order matching pairs by the first time they are encountered while scanning:

list1
then list2
then list3

Implementation Notes

It may be useful to represent an adjacent pair internally using a comparable type such as:

type Pair struct {
    First  int
    Second int
}

This allows a pair to be used as a key while collecting the set of input lists in which it appears.

The public output can still remain:

[]string

as required by the task.

Scalionix Docs

Keyboard Shortcuts

Navigate the documentation without leaving the keyboard.
Navigation
Previous subject
←
Next subject
→
Previous subsection
Alt + ↑
Next subsection
Alt + ↓
Interface
Documentation Home
Ctrl + Enter
Search
Alt + Q
Open shortcuts
?
Close dialog
Esc
Scalionix Docs

Search Documentation