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 1 — Unique and Common Values

Objective

Create a function that analyzes a multi-dimensional integer collection.

The function must identify two different categories of values:

  1. values that occur only once across the complete input
  2. values that are present in every input dimension

Input

The input consists of four integer slices.

List 1

list1 := []int{
    7, 9, 40, 85, 18, 8, 99, 31, 14,
    105, 48, 22, 10, 38, 12, 60, 41, 21,
    115, 15, 6, 33, 20, 17, 13, 35, 75,
}

List 2

list2 := []int{
    70, 100, 31, 60, 90, 41, 55, 33,
    115, 99, 50, 75, 40, 65, 38, 59,
    35, 45, 58,
}

List 3

list3 := []int{
    15, 45, 60, 18, 99, 55, 72, 50,
    38, 7, 65, 119, 70, 95, 6, 115,
    48, 58, 75, 10,
}

List 4

list4 := []int{
    85, 90, 105, 95, 38, 115,
    60, 100, 99, 125, 75, 119,
}

Represent the complete input as:

data := [][]int{
    list1,
    list2,
    list3,
    list4,
}

Result Model

A structured result can be used:

type AnalysisResult struct {
    UniqueValues []int
    CommonValues []int
}

The exact public type may be chosen by the developer, but both result categories must be available independently.

Unique Values

A unique value is a value that occurs exactly once across the entire multi-dimensional collection.

Conceptually:

globalOccurrenceCount(value) == 1

The count is calculated across all input dimensions.

For the provided data, the globally unique values are:

[]int{
    8,
    9,
    12,
    13,
    14,
    17,
    20,
    21,
    22,
    59,
    72,
    125,
}

Common Values

A common value is a value that appears in every input dimension.

For four lists, the condition is:

exists in list1
AND
exists in list2
AND
exists in list3
AND
exists in list4

For the provided input, the common values are:

[]int{
    38,
    60,
    75,
    99,
    115,
}

Expected Result

Conceptually:

AnalysisResult{
    UniqueValues: []int{
        8,
        9,
        12,
        13,
        14,
        17,
        20,
        21,
        22,
        59,
        72,
        125,
    },
    CommonValues: []int{
        38,
        60,
        75,
        99,
        115,
    },
}

Duplicate Values Inside One Dimension

When determining globally unique values, every occurrence matters.

For example:

list1 = {5, 5}
list2 = {}

means that 5 occurs twice globally and is therefore not unique.

When determining common values, the number of occurrences inside one dimension is irrelevant.

The value only needs to exist at least once in every dimension.

Requirements

The function must:

  1. accept a multi-dimensional integer collection
  2. count occurrences across all dimensions
  3. identify values whose global count is exactly one
  4. determine which values exist in every dimension
  5. return both result categories

Result Ordering

The original task does not define an ordering requirement.

For deterministic output, this specification uses ascending numeric order.

Therefore both:

UniqueValues

and:

CommonValues

should be sorted in ascending order.

Empty Input

If the outer collection is empty:

[][]int{}

both result collections should be empty.

If one of several dimensions is empty, no value can be common to every dimension.

Implementation Notes

The two result categories represent different operations:

UniqueValues
    -> global frequency analysis

CommonValues
    -> intersection of all dimensions

They may be calculated independently.

Avoid repeatedly scanning the complete data for every individual value when a frequency or membership structure can provide the same result more efficiently.

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