Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
The Non Repeating Number
(version: 6)
There is an array of only numbers. Every number appears twice in the array except for one. Find the non-repeating number.
Comparing performance of:
Sorted Compare vs Object vs Map vs Sorted indexOf Splice vs Set Sum vs XOR
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var length = 100000; let added = []; var array = new Array(length / 2).fill().map(() => { let number = Math.round(Math.random() * length); do {number = Math.round(Math.random() * length)} while (added.includes(number)); added.push(number); return number; }); array = array.concat(array); array.splice(Math.round(Math.random() * length), 1); var sorted1 = [...array] var sorted2 = [...array] var sum = ((a, b) => a + b) var i
Tests:
Sorted Compare
sorted1.sort() let found, index for(index = 0; index < sorted1.length; index+=2){ if(sorted1[index] !== sorted1[index+1]){ found = sorted1[index] break; } }
Object
let tempObj = {} array.forEach((v) => { if(tempObj[v]) delete tempObj[v] else tempObj[v]= 1 })
Map
let tempMap = new Map() array.forEach((v) => { if(tempMap.has(v)) tempMap.delete(v) else tempMap.set(v, 1) })
Sorted indexOf Splice
let temp = [] let foundIndex sorted2.sort() sorted2.forEach((v) => { foundIndex = temp.indexOf(v) if(foundIndex > -1) temp.splice(foundIndex,1) else temp.push(v) })
Set Sum
let mySet = new Set([...array]) let answer = (2*[...mySet.values()].reduce(sum, 0)) - (array.reduce(sum, 0))
XOR
let res = array[0]; for (i = 1; i < array.length; i++) res = res ^ array[i]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Sorted Compare
Object
Map
Sorted indexOf Splice
Set Sum
XOR
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark test cases and explain what's being tested, along with their pros and cons. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark called "The Non Repeating Number". The goal is to find the non-repeating number in an array where every other number appears twice. This problem can be solved using various data structures such as arrays, objects, maps, sets, or by utilizing bitwise operations. **Test Cases** Each test case represents a different approach to solve the problem: 1. **Sorted Compare**: Uses a sorting algorithm (not specified) to find the non-repeating number. It iterates through the sorted array and checks for pairs of identical numbers. * Pros: Simple implementation, easy to understand. * Cons: Sorting has a high time complexity (O(n log n)), which can lead to slow performance for large arrays. 2. **Object**: Uses an object to store the count of each number in the array. It iterates through the array and increments/decrements the count for each number in the object. * Pros: Efficient way to count occurrences, relatively fast execution time. * Cons: Requires extra memory for storing the object, can be slower than other approaches due to object creation/ updates. 3. **Map**: Similar to the object approach, but uses a map data structure instead. * Pros: Fast execution time, efficient use of memory (as maps are hash-based). * Cons: May not be as intuitive as using an object for this specific problem. 4. **Sorted indexOf Splice**: Uses sorting and then iterates through the sorted array to find the non-repeating number. It uses `indexOf` and `splice` methods, which can be slow due to string manipulation in JavaScript (due to how arrays are implemented in browsers). * Pros: Efficient use of memory, fast execution time. * Cons: Requires extra operations (sorting, `indexOf`, and `splice`) that can lead to performance issues for large arrays. 5. **Set Sum**: Uses a set data structure to find the non-repeating number by calculating the difference between the sum of all numbers in the array and the sum of numbers in the set. * Pros: Efficient use of memory, fast execution time due to set operations. * Cons: May not be as intuitive for beginners due to set operations. **Other Considerations** When choosing an approach, consider factors such as: * Memory usage: Some approaches require more memory than others (e.g., object or map). * Execution time: Factors like sorting, iteration, and data structure operations can impact performance. * Code simplicity: Some approaches might be easier to understand and implement. **Browser-Specific Considerations** The benchmark results show that Chrome 84 on a desktop platform is being tested. This browser version has some known limitations in terms of array manipulation efficiency (e.g., `splice` method). When testing JavaScript benchmarks, keep these browser-specific factors in mind when selecting an approach or optimizing code. Please let me know if you'd like more details or clarification on any aspect!
Related benchmarks:
Array duplicate removal for duplicates exceeding `N`-number
Array duplicate removal for duplicates exceeding `N`-number
Array duplicate removal for duplicates exceeding `N`-number
Array duplicate removal for duplicates exceeding `N`-number
Comments
Confirm delete:
Do you really want to delete benchmark?