Saturday, November 22, 2025
HomeLanguagesJavascriptp5.js NumberDict maxKey() Method

p5.js NumberDict maxKey() Method

The maxKey() method of p5.NumberDict in p5.js is used to find the highest key value in a number dictionary. A key-value pair is a set of two values that are mapped to each other. These values can be accessed by querying this dictionary using the key portion of the pair. A number dictionary can store multiple key-value pairs that can be accessed using the methods of the dictionary.

Syntax: 

maxKey()

Parameters: This function does not accept any parameters.

Return Value: It returns a Number value that is the highest key in the number dictionary.

The example below illustrates the maxKey() method in p5.js:

Example 1:

Javascript




function setup() {
  createCanvas(500, 300);
  textSize(16);
  
  text("Click on the button to add new " +
       "values or get the highest key",
       20, 20);
  
  text("Key:", 20, 60);
  text("Value:", 160, 60);
  
  key_input = createInput('1');
  key_input.position(70, 50);
  key_input.size(40);
  
  val_input = createInput('1');
  val_input.position(220, 50);
  val_input.size(40);
  
  setBtn = createButton("Add new item");
  setBtn.position(30, 100);
  setBtn.mouseClicked(addtoDict);
  
  getBtn = createButton("Get Highest Key");
  getBtn.position(160, 100);
  getBtn.mouseClicked(getHighestKey);
  
  // Create a Number Dictionary initially
  numDict = createNumberDict(100, 0);
}
  
function addtoDict() {
  clear();
  
  let key = int(key_input.value());
  let val = int(val_input.value());
  
  numDict.set(key, val);
  
  text("New key-value added to dictionary",
       20, 160);
  
  text("Key:", 20, 60);
  text("Value:", 160, 60);
  text("Click on the button to add new " +
       "values or get the highest key",
       20, 20);
}
  
function getHighestKey() {
  
  // Get the highest key in the dictionary
  let highestKey = numDict.maxKey();
  
  // Display the highest key
  text("The highest key in the dictionary is: " +
       highestKey, 20, 200);
  
  text("Key:", 20, 60);
  text("Value:", 160, 60);
  text("Click on the button to add new " +
       "values or get the highest key",
       20, 20);
}


Output:

Example 2:

Javascript




function setup() {
  createCanvas(550, 300);
  textSize(16);
  
  text("Click on the button to create a " +
       "new dictionary and get the highest key",
       20, 20);
  
  setBtn = 
    createButton("Create random dictionary");
  setBtn.position(30, 40);
  setBtn.mouseClicked(createNewDict);
  
  getBtn = createButton("Get Highest Key");
  getBtn.position(300, 40);
  getBtn.mouseClicked(getHighestKey);
}
  
function createNewDict() {
  clear();
  
  // Create an object with random values
  let obj = {};
  for (let i = 0; i < 5; i++) {
    let rk = ceil(Math.random() * 100);
    let rn = floor(Math.random() * 100);
    obj[rk] = rn;
  
    text("Key: " + rk + " : Value: " +
         rn, 40, 120 + 20 * i);
  }
  
  // Create a number dict using
  // the above values
  numDict = createNumberDict(obj);
  
  text("New Dictionary created with values",
       20, 80);
  
  text("Click on the button to create a " +
       "new dictionary and get the highest key",
       20, 20);
}
  
function getHighestKey() {
  
  // Get the highest key in the dictionary
  let highestKey = numDict.maxKey();
  
  // Display the lowest key
  text("The highest key in the dictionary is: " +
       highestKey, 20, 240);
  
  text("Click on the button to create a " +
       "new dictionary and get the highest key",
       20, 20);
}


Output:

Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5.NumberDict/maxKey

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6848 POSTS0 COMMENTS