Skip to content

Instantly share code, notes, and snippets.

@nickangtc
Created January 10, 2017 02:25
Show Gist options
  • Select an option

  • Save nickangtc/833debbee00af44750ca14dbf2ba986f to your computer and use it in GitHub Desktop.

Select an option

Save nickangtc/833debbee00af44750ca14dbf2ba986f to your computer and use it in GitHub Desktop.
/*
 * Basically my own implementation of JavaScript's built-in Set object
 * but only accepting integers
 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
 *
 * Create a MySet object that contains an array of integers
 * Check for intersection and union between 2 MySet objects
*/

function MySet () {
  this.numbers = []

  this.has = function (num) {
    if (num && typeof num === 'number') {
      if (this.numbers.includes(num)) {
        return true
      }
    }
    return false
  }

  this.add = function (num) {
    if (!this.has(num)) {
      this.numbers.push(num)
      return true
    } else if (this.has(num)) {
      console.log(`The number ${num} is already part of the set`)
      return false
    }
    return false
  }

  this.remove = function (num) {
    let found = false

    if (num && typeof num === 'number') {
      this.numbers.forEach(function (elem, ind, arr) {
        if (elem === num) {
          arr.splice(ind, 1)

          found = true
        }
      })
    }

    if (found) {
      return true
    } else { return false }
  }

  this.list = function () {
    return this.numbers
  }

  this.intersect = function (anotherMySet) {
    // Validate that object passed in is a MySet() object
    if (this.isMySetObject(anotherMySet)) {
      let result = []

      this.numbers.forEach(function (elem, ind, arr) {
        if (anotherMySet.has(elem)) {
          result.push(elem)
        }
      })

      if (result.length > 0) {
        console.log(`${anotherMySet.list()} intersects with ${this.list()} at ${result}`)
        return result
      } else {
        console.log(`${anotherMySet.list()} does NOT intersect with ${this.list()}`)
        return false
      }
    }
    console.log('Can only check for intersection between two MySet objects')
    return false
  }

  this.union = function (anotherMySet) {
    if (this.isMySetObject(anotherMySet)) {
      let result = this.numbers

      anotherMySet.list().forEach(function (elem, ind, arr) {
        if (!result.includes(elem)) {
          result.push(elem)
        }
      })

      result.sort(function (a, b) {
        return a - b
      })
      console.log(`The union of ${anotherMySet.list()} and ${this.list()} is ${result}`)
      return result
    }
    console.log('Can only check for union between two MySet objects')
    return false
  }

  this.isMySetObject = function (obj) {
    // Check whether passed in object has all same properties as 'this' object
    for (let prop in obj) {
      if (!this.hasOwnProperty(prop)) {
        return false
      }
    }
    return true
  }
}

let set1 = new MySet()
let set2 = new MySet()

// First pass to check all methods
set1.add(5)
set1.add(10)
set1.add(15)
set1.add(20)
set1.has(20)
set1.remove(15)

set2.add(5)
set2.add(1000)

// Check intersect and union methods
set1.intersect(set2)
set1.union(set2)
set2.intersect(set1)
set2.union(set1)

/*
 * To check instance of, can use built-in [if (!(set2 instanceof MySet))]
 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment