Skip to content

A C-based interactive algorithm that counts distinct integers in an array by iteratively 'masking' user-identified elements with a sentinel value (-1) until the dataset is exhausted.

License

Notifications You must be signed in to change notification settings

ShotsMan2/Distinct-Element-Elimination-Algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Distinct Element Elimination Algorithm

This project implements an interactive approach to finding the number of Unique Elements in an integer array. Instead of using standard sorting or hashing sets, it employs an Elimination Game mechanic using a Sentinel Value.

⚙️ Algorithm Logic

The system holds a dataset $S$. The user provides inputs to "clear" the board.

  1. Initialization: The array is populated with mixed integers (duplicates included).
  2. Sentinel Value: We use -1 as a mask to represent "deleted" or "processed" items.
  3. Process Loop:
    • User inputs a number $x$.
    • The algorithm scans the array.
    • IF $x$ is found:
      • All instances of $x$ are replaced with -1.
      • The Distinct Counter is incremented by 1.
    • ELSE: No action is taken (input ignored).
  4. Termination: The loop breaks when the entire array consists of -1s.

Visual State Transition

Initial: [1, 6, 5, 2, 1, 4, 2]

  • User Input: 1
  • Action: Mask all 1s. Increment count. State 1: [-1, 6, 5, 2, -1, 4, 2]
  • User Input: 2
  • Action: Mask all 2s. Increment count. State 2: [-1, 6, 5, -1, -1, 4, -1]

🚀 Usage

  1. Compile the code:
    gcc main.c -o elimination_counter
  2. Run the executable:
    ./elimination_counter
  3. Guess numbers until the program terminates and reports the count of unique numbers.

This repository demonstrates array modification and interactive state checking in C.

About

A C-based interactive algorithm that counts distinct integers in an array by iteratively 'masking' user-identified elements with a sentinel value (-1) until the dataset is exhausted.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages